Skip to main content
gamedev.net
🔒 Locked

Polymorphism

Started by Digital Extent Jan 4, 2003 at 4:19 PM 1 replies 150+ views
Original Post
Digital Extent
Digital Extent
Consider the situation:
  
class Base
{
public:
virtual ~Base(){}
virtual void Func1() = 0;
};

class Sub : public Base
{
public:
virtual ~Sub() {}
virtual void Func1() { //something }

void Func2() { //something }

};

//Main program


Base* pClass;
pClass = new Sub;

//pClass->Func2(); won''t compile

  
Is there anyway around this?
Programming is easy, thinking is hard.
noVum
noVum
No for good reason.

What if you would make an array of Base and you would try to call Func2() of every class in that array. There could be classes without Func2...
krez
krez
you''d have to include Func2() as a virtual function in the base class, since you are trying to call it from the base pointer.

you could cast the pointer to a Sub (derived class) pointer, but that would be bad style.
--- krez ([email="krez_AT_optonline_DOT_net"]krez_AT_optonline_DOT_net[/email])

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.