Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Accessing inherited operator<< in base class from derived class

Reply
Thread Tools

Accessing inherited operator<< in base class from derived class

 
 
Alicia
Guest
Posts: n/a
 
      11-23-2004
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:stream& operator<<(std: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 CostedCallublic 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:perator<<(out,phonecall);

return out;
}

I am completely lost. Any help would be appreciated.

Alicia
 
Reply With Quote
 
 
 
 
Rob Williscroft
Guest
Posts: n/a
 
      11-23-2004
Alicia wrote in news:(E-Mail Removed) om in
comp.lang.c++:

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


It isn't a member so you can't do this:

> PhoneCall:perator<<(out,phonecall);


out << static_cast< Phoncall const & >( phonecall );
>
> return out;
> }
>


Rob.
--
http://www.victim-prime.dsl.pipex.com/
 
Reply With Quote
 
 
 
 
Scuro
Guest
Posts: n/a
 
      11-24-2004
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:perator<<" << endl;
return out;
}
};

And now the class B!

class B : public A {
public:
// code
ostream& print(ostream& out) const {
out << "B: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:rint(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: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...
 
Reply With Quote
 
jjr2004a
Guest
Posts: n/a
 
      11-24-2004
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:ut()
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;
}
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Accessing derived class members from base class Bhawna C++ 7 08-26-2008 11:03 AM
Accessing Base class function using a pointer to a derived class Abhijit Deshpande C++ 7 07-16-2008 08:27 PM
'Class.inherited' v. 'inherited' syntax inside Class 7stud -- Ruby 11 11-09-2007 06:45 PM
Derived::Derived(const Base&) and Derived& operator=(const Base&) developereo@hotmail.com C++ 1 05-23-2007 01:44 PM
Derived::Derived(const Base&) and Derived& operator=(const Base&) developereo@hotmail.com C++ 1 05-23-2007 12:07 AM



Advertisments