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 Bublic A
{
public:
enum Command
{
Send
};
using A:perator<<;
virtual bool operator<<(Command) = 0;
};
class Cublic 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
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 Bublic A
{
public:
enum Command
{
Send
};
using A:perator<<;
virtual bool operator<<(Command) = 0;
};
class Cublic 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