Overloading and Inheritance with virtual member functions

J

jois.de.vivre

Here is my problem:

I have a base class A with operator<< defined to take a template
parameter and append the parameter to a output string stream
(ostringstream). I have a derived class B, which inherits from A and
defines a pure virtual output operator<< taking as a parameter an
enumeration type 'Command' (which is defined in B itself). The point
of the 'Command' enumeration is to have a different implementation of
operator<< when the user sends it an argument of type 'Command'. I
have a final derived class C, which inherits from B and implements
operator<< for parameter type 'Command'. Here is the code describing
my situation (my question to follow after the code):


/* -------- begin code ------------- */
#include <iostream>
#include <sstream>
using namespace std;

class A
{
public:
template<class T>
A &operator<<(const T &Data)
{
OutputBuffer << Data;
}

protected:
ostringstream OutputBuffer;
};

class B:public A
{
public:
enum Command
{
Send
};
using A::eek:perator<<;
virtual bool operator<<(Command) = 0;
};

class C:public B
{
public:
bool operator<<(Command)
{
cout << "You have chosen to send: "
<< OutputBuffer.str() << endl;
}
};

int main(int argc, char *argv[])
{
C c;
B &b = c;

/* (1) */ b << "This is a message" << Send;
}

/* -------- end code ------------- */


My expectation was that (1) would cause operator<< from class A to be
used for the string type and operator<< from class C to be used for
the 'Command' type. However, I find that operator<< from class A is
used for both. Why does this occur?

Thanks

Prashant
 
V

Victor Bazarov

Here is my problem:

I have a base class A with operator<< defined to take a template
parameter and append the parameter to a output string stream
(ostringstream). I have a derived class B, which inherits from A and
defines a pure virtual output operator<< taking as a parameter an
enumeration type 'Command' (which is defined in B itself). The point
of the 'Command' enumeration is to have a different implementation of
operator<< when the user sends it an argument of type 'Command'. I
have a final derived class C, which inherits from B and implements
operator<< for parameter type 'Command'. Here is the code describing
my situation (my question to follow after the code):


/* -------- begin code ------------- */
#include <iostream>
#include <sstream>
using namespace std;

class A
{
public:
template<class T>
A &operator<<(const T &Data)
{
OutputBuffer << Data;
}

protected:
ostringstream OutputBuffer;
};

class B:public A
{
public:
enum Command
{
Send
};
using A::eek:perator<<;
virtual bool operator<<(Command) = 0;
};

class C:public B
{
public:
bool operator<<(Command)
{
cout << "You have chosen to send: "
<< OutputBuffer.str() << endl;
}
};

int main(int argc, char *argv[])
{
C c;
B &b = c;

/* (1) */ b << "This is a message" << Send;
}

/* -------- end code ------------- */


My expectation was that (1) would cause operator<< from class A to be
used for the string type and operator<< from class C to be used for
the 'Command' type. However, I find that operator<< from class A is
used for both. Why does this occur?

What _type_ does (b << "This is a message") return?
What operator << functions are defined for that type?

V
 
K

Kishore Yada

Here is my problem:

I have a base class A with operator<< defined to take a template
parameter and append the parameter to a output string stream
(ostringstream). I have a derived class B, which inherits from A and
defines a pure virtual output operator<< taking as a parameter an
enumeration type 'Command' (which is defined in B itself). The point
of the 'Command' enumeration is to have a different implementation of
operator<< when the user sends it an argument of type 'Command'. I
have a final derived class C, which inherits from B and implements
operator<< for parameter type 'Command'. Here is the code describing
my situation (my question to follow after the code):


/* -------- begin code ------------- */
#include <iostream>
#include <sstream>
using namespace std;

class A
{
public:
template<class T>
A &operator<<(const T &Data)
{
OutputBuffer << Data;
}

protected:
ostringstream OutputBuffer;
};

class B:public A
{
public:
enum Command
{
Send
};
using A::eek:perator<<;
virtual bool operator<<(Command) = 0;
};

class C:public B
{
public:
bool operator<<(Command)
{
cout << "You have chosen to send: "
<< OutputBuffer.str() << endl;
}
};

int main(int argc, char *argv[])
{
C c;
B &b = c;

/* (1) */ b << "This is a message" << Send;
}

/* -------- end code ------------- */


My expectation was that (1) would cause operator<< from class A to be
used for the string type and operator<< from class C to be used for
the 'Command' type. However, I find that operator<< from class A is
used for both. Why does this occur?

Thanks

Prashant

The following statement is evaluated left to right.

b << "This is a message" << Send;

First ( b << "This is a message" ) is evaluated. It will return a A&
( reference to A )

Then (ref. to A) << Send; is evaluated. In class A, operator<< is not
virtual, so class A's operator<< is called
 
J

jois.de.vivre

Here is my problem:
I have a base class A with operator<< defined to take a template
parameter and append the parameter to a output string stream
(ostringstream). I have a derived class B, which inherits from A and
defines a pure virtual output operator<< taking as a parameter an
enumeration type 'Command' (which is defined in B itself). The point
of the 'Command' enumeration is to have a different implementation of
operator<< when the user sends it an argument of type 'Command'. I
have a final derived class C, which inherits from B and implements
operator<< for parameter type 'Command'. Here is the code describing
my situation (my question to follow after the code):
/* -------- begin code ------------- */
#include <iostream>
#include <sstream>
using namespace std;
class A
{
public:
template<class T>
A &operator<<(const T &Data)
{
OutputBuffer << Data;
}
protected:
ostringstream OutputBuffer;
};
class B:public A
{
public:
enum Command
{
Send
};
using A::eek:perator<<;
virtual bool operator<<(Command) = 0;
};
class C:public B
{
public:
bool operator<<(Command)
{
cout << "You have chosen to send: "
<< OutputBuffer.str() << endl;
}
};
int main(int argc, char *argv[])
{
C c;
B &b = c;
/* (1) */ b << "This is a message" << Send;
}
/* -------- end code ------------- */
My expectation was that (1) would cause operator<< from class A to be
used for the string type and operator<< from class C to be used for
the 'Command' type. However, I find that operator<< from class A is
used for both. Why does this occur?

Prashant

The following statement is evaluated left to right.

b << "This is a message" << Send;

First ( b << "This is a message" ) is evaluated. It will return a A&
( reference to A )

Then (ref. to A) << Send; is evaluated. In class A, operator<< is not
virtual, so class A's operator<< is called

Excellent! Thank you, that makes perfect sense.
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top