• Advertisement

Archived

This topic is now archived and is closed to further replies.

Overloaded functions in derived classes

This topic is 5980 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hey i want to know what would happen if i did this... i guess the best way to explain it is with some code:
  
class BaseClass
{
 public:
 virtual void Func();
};

class DerivedClass : publiC BaseClass
{
 public:
 void Func();
};

BaseClass *base;
DerivedClass derived;

base = &derived;

  
Now if i call ''Func()'' from ''base'', will it call DerivedClass''s overloaded ''Func()'' since it is pointing to a derived class or will it call BaseClass''s ''Func()'' because it is of BaseClass type? "If people were perfect, what would the point of life be?" -- meself

Share this post


Link to post
Share on other sites
Advertisement
Well, you could''ve just tried it .
It will call DerivedClass''s func() because BaseClass''s func() is virtual.

~~~~~~~~~~
Martee

Share this post


Link to post
Share on other sites
Clarification: ''base'' is Not of ''BaseClass'' type. It is of ''BaseClass*'' type, ie. a pointer to a BaseClass. Accessing methods through a pointer (or reference, I believe) will use virtual methods, whereas accessing methods directly through an object should use that object''s method directly. So in your example, it calls the DerivedClass''s method, since the pointer is to a derived class, and the method is declared in the base class as virtual.

Share this post


Link to post
Share on other sites

  • Advertisement