473,387 Members | 1,486 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Overloading and Inheritance with virtual member functions

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::operator<<;
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

Feb 7 '07 #1
3 1529
jo***********@gmail.com wrote:
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::operator<<;
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Feb 7 '07 #2

jo***********@gmail.com wrote:
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::operator<<;
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

Feb 7 '07 #3
On Feb 7, 2:40 am, "Kishore Yada" <yadakish...@gmail.comwrote:
jois.de.vi...@gmail.com wrote:
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::operator<<;
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
Excellent! Thank you, that makes perfect sense.

Feb 7 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: qazmlp | last post by:
class base { // other members public: virtual ~base() { } virtual void virtualMethod1()=0 ; virtual void virtualMethod2()=0 ; virtual void virtualMethod3()=0 ;
19
by: Mike Tyka | last post by:
Hello community, i'm fairly new to using the STL but i've been experimenting a bit with it. I tried to derive a new class say MyString from string like so: class MyString: public string{...
6
by: woosu | last post by:
Hello ladies and gentlemen. I have a relatively simple problem that I've been unable to solve. In the interest of learning C++, I've decided to write a simple game. The basis for the game is...
5
by: Noah Roberts | last post by:
Is there anything that says that if you virtually inherit from one class you have to virtually inherit from anything you inherit from?
12
by: mijobee | last post by:
I'm very new to c++ and just writing some code to learn. I've run into a problem, with a javaish design, and want to know if there is any possible solution without modifying the design. I've read...
10
by: Mihai Osian | last post by:
Hi everyone, Given the code below, can anyone tell me: a) Is this normal behaviour ? b) If it is, what is the reason behind it ? I would expect the A::method(int) to be inherited by B. ...
5
by: toton | last post by:
Hi, I want a few of my class to overload from a base class, where the base class contains common functionality. This is to avoid repetition of code, and may be reducing amount of code in binary,...
3
by: Chameleon | last post by:
What is better if you want upcasting in intermediate classes like below? Multiple Inheritance and Overloading or simply RTTI? RTTI wants time but MI and Overloading create big objects (because of...
23
by: Dave Rahardja | last post by:
Since C++ is missing the "interface" concept present in Java, I've been using the following pattern to simulate its behavior: class Interface0 { public: virtual void fn0() = 0; };
8
by: yashwant pinge | last post by:
#include<iostream> using namespace std; class base { public: void display() { } };
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.