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

## 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 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 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 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.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
627734
• Total Posts
2978840

• 10
• 9
• 21
• 14
• 12