Click to See Complete Forum and Search --> : Polymorphic not?


rliq
January 17th, 2003, 08:14 PM
I have the following piece of 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.

PaulWendt
January 17th, 2003, 08:19 PM
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

rliq
January 17th, 2003, 08:24 PM
Yes, it is declared virtual:


virtual bool DoPropertiesDlg( ... ) { ASSERT(false); return false; };

rliq
January 17th, 2003, 08:59 PM
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.