Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling ostream friend function of base class in c++

Tags:

c++

iostream

So, I have two classes:

class Base {
    private:
        int number;
    public:
        friend ostream & operator<<(ostream & output, const Base &n);
}

ostream & operator<<(ostream & output, const Base &n) {
    output<<n.a<<endl;
    return output;
}

class Child : Base {
    private:
        int second;
    public:
        friend ostream & operator<<(ostream & output, const Child &n);

}

ostream & output<<(ostream & output, const Child &n) {
    output<<n.second<<Base:: ????<<endl;
    return output;
}

My question is, how can i call the friend function of the base class from the child class to output its content:

output<<n.second<<Base:: ????<<endl

Thanks in advance :)

like image 631
mjekov Avatar asked Apr 28 '12 20:04

mjekov


1 Answers

output<<n.second<<static_cast<const Base&>(n)<<endl;
like image 170
David Avatar answered Sep 21 '22 12:09

David