Jacob Lund Fisker wrote:
> On Wed, 17 Oct 2007, Mickey Mathieson wrote:
> 
>>> Yes. And also, which object on the heap is pu
>>> pointing to? It seems to me
>>> that pointers that are declared to point to a base
>>> class should not be
>>> able to point to inherited classes too without
>>> problems with e.g. pointer
>>> arithmetic?!
>>>
>> a pointer to a derived class is type-compatible with a
>> pointer to its base class. Polymorphism.
>>
>> The pu pointer is pointing to the Over object.
> 
> Yet it can't access any methods in Over that aren't in Under, right? So 
> what is the point of doing that? I just can't see what the benefits are of 
> syntactically allowing it in the first place?!

The 'virtual' keyword is what makes base/derived classes useful (i.e. 
virtual functions).  I highly recommend reading Safe C++ Design 
Principles.  It is free for c-prog members in the Files section.

BTW, the way you wrote the class is incorrect.  When defining base and 
derived classes, you must declare a virtual destructor (even if it is 
empty) or you'll probably end up with a memory leak somewhere along the 
line.

-- 
Thomas Hruska
CubicleSoft President
Ph: 517-803-4197

*NEW* MyTaskFocus 1.1
Get on task.  Stay on task.

http://www.CubicleSoft.com/MyTaskFocus/

Reply via email to