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

Started by
2 comments, last by deadimp 18 years, 7 months ago
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]
Projects:> Thacmus - CMS (PHP 5, MySQL)Paused:> dgi> MegaMan X Crossfire
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.
Projects:> Thacmus - CMS (PHP 5, MySQL)Paused:> dgi> MegaMan X Crossfire
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
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.
Projects:> Thacmus - CMS (PHP 5, MySQL)Paused:> dgi> MegaMan X Crossfire

This topic is closed to new replies.

Advertisement