Accessing inherited operator<< in base class from derived class

A

Alicia

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::eek:stream& operator<<(std::eek:stream& 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::eek:perator<<(out,phonecall);

return out;
}

I am completely lost. Any help would be appreciated.

Alicia
 
S

Scuro

Alicia said:
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::eek:perator<<" << endl;
return out;
}
};

And now the class B!

class B : public A {
public:
// code
ostream& print(ostream& out) const {
out << "B::eek:perator<<" << 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::eek:perator<<" << 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...
 
J

jjr2004a

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;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top