Overloading the output operator for both the base class and its derived class (C++)
<p>this is a question on overloading the output operator in C++:
How do you overload &lt;&lt; on BOTH the base class and the derived class? Example:</p>

<pre><code>#include &lt;iostream&gt;
#include &lt;cstdlib&gt;
using namespace std;


class Base{
public:
virtual char* name(){ return "All your base belong to us.";}
};

class Child : public Base{
public:
virtual char* name(){ return "All your child belong to us.";}
};


ostream&amp; operator &lt;&lt; (ostream&amp; output, Base &amp;b){
output &lt;&lt; "To Base: " &lt;&lt; b.name() ;
return output;}

ostream&amp; operator &lt;&lt; (ostream&amp; output, Child &amp;b){
output &lt;&lt; "To Child: " &lt;&lt; b.name() ;
return output;}


int main(){

Base* B;
B = new Child;

cout &lt;&lt; *B &lt;&lt; endl;

return 0;

}
</code></pre>

<p>The output is</p>

<pre><code>To Base: All your child belong to us."
</code></pre>

<p>so the name() in Child shadowed that of Base; but the overloading of &lt;&lt; does not descend from Base to Child. How can I overload &lt;&lt; such that, when its argument is in Base it uses ONLY the Base version of &lt;&lt; ?</p>

<p>I want "To Child: All your child belong to us." to be output in this case.</p>
