By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,301 Members | 3,575 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,301 IT Pros & Developers. It's quick & easy.

Accessing inherited operator<< in base class from derived class

P: n/a
Hello,
I am trying to figure out how to call an overloaded operator<<
inherited from a base class.

#ifndef PHONECALL
#define PHONECALL

#include "time.h"
#include "interval.h"

enum CallType {LOCAL, LONG_DISTANCE};

class PhoneCall
{
public:
friend std::istream& operator>>(std::istream& in, PhoneCall&
phoneCall);
friend std::ostream& operator<<(std::ostream& out, const
PhoneCall& phoneCall);
CallType getCallType() const;
int callLength() const;

private:
CallType callType;
Interval<Time> interval;
};

#endif
Here is the inherited class:
#include "phonecall.h"
#include "money.h"
#include <iostream>

class CostedCall:public PhoneCall
{
public:
CostedCall();
CostedCall(PhoneCall phoneCall, Money cost);
friend ostream& operator<<(ostream& out, const CostedCall&
phonecall);
private:
Money cost;
};

The output on the base class PhoneCall works correctly. What I need to
know is how to call the operator<< in the base class PhoneCall from
the derived class CostedCall. I tried something like this:

ostream& operator<<(ostream& out,const CostedCall& phonecall)
{

PhoneCall::operator<<(out,phonecall);

return out;
}

I am completely lost. Any help would be appreciated.

Alicia
Jul 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Alicia wrote in news:78**************************@posting.google.c om in
comp.lang.c++:

ostream& operator<<(ostream& out,const CostedCall& phonecall)
{

It isn't a member so you can't do this:
PhoneCall::operator<<(out,phonecall);
out << static_cast< Phoncall const & >( phonecall );
return out;
}


Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #2

P: n/a
Alicia wrote:
Hello,
I am trying to figure out how to call an overloaded operator<<
inherited from a base class. *snip* I am completely lost. Any help would be appreciated.

Alicia


Nice question :)

So, let me simplify your problem:

class A {
public:
friend ostream& operator<<(ostream& out, const A& a){
// code
}
// code
};

class B : public A {
public:
// code
friend ostream& operator<<(ostream& out, const B& b){
// code
}
}

So, what you want to do is something like that:

A *a=new B();
cout << *a;
and you want the operator<< in class B to be called, ok?
but you can not redefine a function that is not virtual, so you want to
write:

class A { // version 1.1
public:
virtual friend ostream& operator<<(ostream& out, const A& a){
// code
}
// code
};

Ops, you can not make friends virtual, and now?

class A { // version 1.2
public:
virual ostream& operator<<(ostream& out, const A& a){
// code
}
// code
};

Ops! operator<< now cannot access private members of A!
The question is: how to make a virtual and friend function? The answer
is: separate them!!!

class A { // version 2.0
public:
friend ostream& operator<<(ostream& out, const A& a){
return a.print(out);
}
virtual ostream& print(ostream& out) const {
out << "A::operator<<" << endl;
return out;
}
};

And now the class B!

class B : public A {
public:
// code
ostream& print(ostream& out) const {
out << "B::operator<<" << endl;
return out;
}
}

Now, what happens when you do:

A *a=new B();
cout << *a;

1) operator<< (ostream&, A&) is called.
2) a.print(out) is called, but the type of a is B, so B::print(ostream&)
is called.

If you need, you can complete the code adding the operator<< to B too.

class B : public A {
public:
// code
friend ostream& operator<<(ostream& out, const B& b){
return b.print(out);
}
ostream& print(ostream& out) const {
out << "B::operator<<" << endl;
return out;
}
}

This tecnique is called multiple dispatching and it's explained well in
"thinking in C++".

Bye!

P.S.: I have not compiled the code, so i apologize for errors...
Jul 22 '05 #3

P: n/a
A better way to do this is in section 21.2.3.1 "Virtual
Output Functions" in Stroustrup, 3rd Edition.

He suggests using a virtual put function which in turn
used in an operator<< function defined for the base class.

Here's his example with some added comments:

// base class header file
//
class My_base {
public:
virtual ostream& put(ostream& s) const=0
};

ostream& operator<<(ostream& s, const My_base& r)
{
// uses the right put()
return r.put(s);
}

// derived class header file
//
class Sometype : public My_base {
public:
// override My_base::put()
ostream& put(ostreasm& s) const;
};
// some other .cpp file
//
void f(const My_base& r, Sometype& s)
{
// use << which calls the right put()
std::cout << r << s;
}
Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.