-
Polymorphic not?
I have the following piece of code:
Code:
CLocation* CLocation::CreateNewLocation( ... ) // Static Function
{
// May return pointers to different classes derived from
// CLocation, but for this example:
return new CShapeLocation();
}
...
// Main Code
CLocation* pLocation = CLocation::CreateNewLocation( ... );
pLocation->DoPropertiesDlg() ;
But...
CLocation:: DoPropertiesDlg()
is ALWAYS called, not..
CShapeLocation:: DoPropertiesDlg()
as I would have expected. What am I doing wrong?
Thanks
Rob.
-
You neglected to produce the most important piece of code: the
declaration of the DoPropertiesDlg() function. Is it possible that
you forgot to declare the DoPropertiesDlg() function virtual? If
you did not, you'd better make the destructor virtual as well.
--Paul
-
Yes, it is declared virtual:
Code:
virtual bool DoPropertiesDlg( ... ) { ASSERT(false); return false; };
-
I found what my problem was.
The derived function was not declared EXACTLY the same as the base one. One of the parameters in the base function was a CLocation*. In the derived I had declared it as CShapeLocation*. My mistake.
Thanks
Rob.