Jump to content
  • Advertisement
Sign in to follow this  
deadimp

More C++ Polymorphism - Calling A Function Up a Hiearchy...

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

Is there some way to call the highest function up in a C++ polymorphic hierarchy? Example:
class Base {
public:
 virtual Base* NewSelf() { return NULL; }
 Base* Copy() {
  Base *out=NewSelf(); //How can I call the higher up functions of 'NewSelf()' here?
  if (out==NULL) return out;
  *out=this;
  return out;
 }
};

class SubA : public Base {
public:
 Base* NewSelf() { return new SubA; }
};

class SubB : public Base {
public:
 Base* NewSelf() { return new SubB; }
};
[Please note that this isn't exactly what I'm trying to do, but it's close] Well, as I was typing this, I though of something: Would "this->NewSelf()" call the higher functions? It seems like that would make sense, but I don't know... [Goes to test it]

Share this post


Link to post
Share on other sites
Advertisement
Don't say it... Please, don't say it.
I know this was a retarded question. I had forgotten that when a class executes it's virtual functions, it executes them at the highest scope, and that you have to use scoping to call the LOWER functions, not the HIGHER ones.

Share this post


Link to post
Share on other sites
If the base had access to the derrived type and could then upcast the 'this' pointer to the derrived type you might be able to access its methods.

Just a thought and probably wrong.

ace

Share this post


Link to post
Share on other sites
Actually, it calls the highest function by default. The only time you would need to cast or scope is when you are calling a lower function.
Thanks for the help.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • 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!