By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,264 Members | 1,795 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,264 IT Pros & Developers. It's quick & easy.

Which print function going to be called. scope question.

P: n/a
class Base{

public:
std::ostream& operator<<( std::ostream& os, const string &str )
{ return print(os); }

void print(std:ostream& os){ os<<"Base\n";}
};
class Derived{
public:
using Base::operator<<;
void print(std:ostream& os){ os<<"Derived\n";}
};

D d=Derived;
D<<"str";

B & pd=new Derived;
pd<<"str";
Notice that that print is not a virtual function.

I wonder if you bring base class member in the Derive class scope
and invoke it via Derived object, which print is it going to call: one
from derived or one from base.
Jun 27 '08 #1
Share this Question
Share on Google+
1 Reply


P: n/a
sasha wrote:
class Base{

public:
std::ostream& operator<<( std::ostream& os, const string &str )
{ return print(os); }

void print(std:ostream& os){ os<<"Base\n";}
};
class Derived{
public:
using Base::operator<<;
void print(std:ostream& os){ os<<"Derived\n";}
};

D d=Derived;
D<<"str";

B & pd=new Derived;
pd<<"str";
Notice that that print is not a virtual function.

I wonder if you bring base class member in the Derive class scope
and invoke it via Derived object, which print is it going to call: one
from derived or one from base.
Sasha, please post real code. Your 'print' functions are 'void', so
you cannot 'return print(os)' from the operator <<.

Generally speaking, making Base::operator<< "visible" in Derived is
unnecessary, it's visible as it is. And, of course, such visibility
change does NOT affect the behaviour of the 'operator<<' function.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 27 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.