I need to use a virtual << operator. However, when I try to write:
virtual friend ostream & operator<<(ostream& os,const Advertising& add);
I get the compiler error
Error 1 error C2575: 'operator <<' : only member functions and bases can be virtual
How can I turn this operator virtual?
You define your operator << to call a virtual print method:
The problem with this setup is that the operator<< you defined above is a free function, which can't be virtual (it has no receiver object). In order to make the function virtual, it must be defined as a member of some class, which is problematic here because if you define operator<< as a member of a class then the operands will be in the wrong order:
means that
will not compile, but
will be legal.
To fix this, you can apply the Fundamental Theorem of Software Engineering - any problem can be solved by adding another layer of indirection. Rather than making operator<< virtual, consider adding a new virtual function to the class that looks like this:
Then, define operator<< as
This way, the operator<< free function has the right parameter order, but the behavior of operator<< can be customized in subclasses.
Hope this helps!
It looks like you really want to provide output functionality for a hierarchy of classes, and if so, you can provide a
friend operator <<
that calls avirtual
function.Also detailed in the C++ FAQ