overload & and inheritance problem

V

Vincent ROGER

I have a maybe a "noob" question to ask but I'd like to why that code does
not compile and why more precisely it does not search for 'print()' method
in 'Model'.
Of course I could replace 'print()' by 'write()' but I'm stubborn.

#include <iostream>

using namespace std;

struct Model {
virtual void print(ostream &o)=0;
void print() { print(cout); }
};

struct Subclass : public Model {
void print(ostream& o)
{
o << "Subclass";
}

};

int main()
{
Subclass c;

c.print();
}

Thanks for any help.
 
V

Victor Bazarov

Vincent said:
I have a maybe a "noob" question to ask but I'd like to why that code
does not compile and why more precisely it does not search for
'print()' method in 'Model'.
Of course I could replace 'print()' by 'write()' but I'm stubborn.

Subclass::print(ostream&) hides Model::print(). See below.

Add

using Model::print;
Thanks for any help.

Anytime.

V
 
V

Vincent ROGER

Thank you for both the explanation and the solution.
Seems like C++ will always be able to surprise me.
 

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

Staff online

Members online

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,007
Latest member
obedient dusk

Latest Threads

Top