Jump to content
  • Advertisement

Archived

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

LarrxX

Mysteries of polymorphism

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

I just wanted to know if this was ''normal'' behavior (is this in the C++ norm) or if it depends on the compiler (both .NET and g++ 3.3.2 have hte same problem). Suppose I have the following: class BaseClass { virtual void myMethod( void ); }; class ChildClass : public BaseClass { //Some stuff here }; class ProblemClass : public BaseClass { //Another use for myMethod virtual void myMethod( int ); }; int main( int argc, char* argv[] ) { ChildClass* child = new ChildClass(); ProblemClass* problem = new ProblemClass(); child->myMethod(); //Ok, no problem here problem->myMethod( 2 );//No problem here either problem->myMethod(); //Compile error!! Undefined method! ((BaseClass*)problem)->myMethod(); //No problem here } So my question is: is it normal that if in a derived class, I define a method with the same name but different parameters (and thus a different signature), is it normal that this method shadows the method in the base class? I''m not asking for a solution here, it''s pretty straightforward, but I just want to know WHY?! WHYYYYYYYYYYYY?!!!! Thanks, LarrxX

Share this post


Link to post
Share on other sites
Advertisement
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
yes, it's normal.


class ProblemClass : public BaseClass
{
//Another use for myMethod

virtual void myMethod( int );
using BaseClass::myMethod;
};


Putting using BaseClass::myMethod brings it into scope and it will be used in resolving names.

[edited by - quorn on June 2, 2004 11:13:14 AM]

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!