Assistance with inheritance issue in C++ : <p>I have a 2 C++ classes(A and B), let's say B inherits from A. </p>

<p>They both need to have a toString() function. They are being called from a function that returns the base class type. After returning, I want to call the tostring() function for the correct type of class. If the function returns a B type object, I want to call the toString() from the B class.</p>

<p>I think my problem is coming from the fact that the function returns a reference to the base class so it is calling the function from the base class.</p>

<p>Example classes:</p>

<pre><code>class A
{
  std::string toString();
};

class B
 : public A
{
  int extraThingToPrint;
  std::string toString(); //prints a different message than the A version
};
</code></pre>

<p>Example function:</p>

<pre><code>A otherClass::scan()
{

   if(otherVar == 'a') return A();
   else if(otherVar == 'bb') return B();
}

std::cout &lt;&lt; scan().toString(); //if bb plz print B.toString() and not A.toString() (but if a, use A.toString())
</code></pre>
