Jump to content
  • Advertisement

Archived

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

greldik

Polymorphism and inheritance

This topic is 5274 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

Hi I'm not exactly a newbee when it comes to programming C/C++, but one thing I can't figure out! Why doesn't polymorphism and inheritance work together? Example:
class A
{
public:
    void Func(int a);
};
class B : public A
{
public:
    void Func(B &a);
};

B aBInstance;
aBInstance.Func(10);
That last line will give the error: "cannot convert parameter 1 from 'const int' to 'class B &'" To make this compile you have to write: aBInstance.A::Func(10) Why can't the compiler figure that out by itself? // Martin [edited by - greldik on June 5, 2004 11:49:09 PM]

Share this post


Link to post
Share on other sites
Advertisement
Your function is neither virtual nor does it have the same signature in A and B, so the version in B hides the one in A and there is no polymorphism involved whatsoever.


“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.”
— Brian W. Kernighan

Share this post


Link to post
Share on other sites
In C++ a name in a derived class hides any member variable or function of the same name in its base class(es).

There are several reasons for why this is the case, but in a nutshell, it''s because there are often times where having the names from the base classes merged into the derived class''s scope causes subtle bugs. Therefore, by default the scopes are separate, and C++ provides the using declaration to allow the scopes to merge if the class designer is certain that there will be no issues.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!