Sign in to follow this  

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

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

This topic is 4486 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this