Calling a method with the pointer to a base class
-
I´m sure there is a simple solution but it drives me crazy:
class A
{ virtual bool method M() };class B : public A { .... }
class C1 : public B
{ virtual bool method M() override };
class C2 : public B
{ virtual bool method M() override };class D
{
static bool DoSomething( A* ptr ) { ptr -> M(); }
}Now I am working in a codepiece of C1 which calls D::DoDomething( )
C1::anymethod()
{
D::DoSomething( this );
}Ok, thats simple: C1::M() will be called because of the inheritance But now I wanna have that D calls the M() of C1´s base A by typecasting the this ptr to a base:
C1::anymethod()
{
D::DoSomething( (A*)this );
}but this will also call C1:M(). How can I tell the D::DoSomething function "If you use M1() then you have to call A::M1() " with knowing that in DoSomething by the type of the ptr?
-
I´m sure there is a simple solution but it drives me crazy:
class A
{ virtual bool method M() };class B : public A { .... }
class C1 : public B
{ virtual bool method M() override };
class C2 : public B
{ virtual bool method M() override };class D
{
static bool DoSomething( A* ptr ) { ptr -> M(); }
}Now I am working in a codepiece of C1 which calls D::DoDomething( )
C1::anymethod()
{
D::DoSomething( this );
}Ok, thats simple: C1::M() will be called because of the inheritance But now I wanna have that D calls the M() of C1´s base A by typecasting the this ptr to a base:
C1::anymethod()
{
D::DoSomething( (A*)this );
}but this will also call C1:M(). How can I tell the D::DoSomething function "If you use M1() then you have to call A::M1() " with knowing that in DoSomething by the type of the ptr?
The simplest solution would be to create another non-virtual function in class A:
class A
{
bool method AM();
virtual bool method M() { return AM(); }
};class D
{
static bool DoSomething( A* ptr ) { ptr ->AM(); }
}; -
The simplest solution would be to create another non-virtual function in class A:
class A
{
bool method AM();
virtual bool method M() { return AM(); }
};class D
{
static bool DoSomething( A* ptr ) { ptr ->AM(); }
};oh , the example I have is more tricky: D should call "the next M() of any parent above the child class C1" Its vice versa to the virtual inheritance when you call the last one in the hierarchy ... if I call
C1::anytest ()
{
B::M();
}it goes "up" the hierarchy an calls the next found M(); in this case A::M() But in class D´s DoSomething where I get a ptr to C1, or C2 ... how can do it there?
-
oh , the example I have is more tricky: D should call "the next M() of any parent above the child class C1" Its vice versa to the virtual inheritance when you call the last one in the hierarchy ... if I call
C1::anytest ()
{
B::M();
}it goes "up" the hierarchy an calls the next found M(); in this case A::M() But in class D´s DoSomething where I get a ptr to C1, or C2 ... how can do it there?
Sorry, this was not clear from your initial question:
Quote:
But now I wanna have that D calls the M() of C1´s base A
But you can do the same: Implement a non-virtual function
B::BM()
which is called by the virtual functionB::M()
and so on. Then you can call the non-virtual functionsAM()
,BM()
, and so on or the virtualM()
function. If you want to call theM()
function of the next upper class in the hierarchy, you need to know the real class of the passed argument during run-time. This requires that your classes support some kind of Run-time type information - Wikipedia, the free encyclopedia[^]. -
oh , the example I have is more tricky: D should call "the next M() of any parent above the child class C1" Its vice versa to the virtual inheritance when you call the last one in the hierarchy ... if I call
C1::anytest ()
{
B::M();
}it goes "up" the hierarchy an calls the next found M(); in this case A::M() But in class D´s DoSomething where I get a ptr to C1, or C2 ... how can do it there?
-
oh , the example I have is more tricky: D should call "the next M() of any parent above the child class C1" Its vice versa to the virtual inheritance when you call the last one in the hierarchy ... if I call
C1::anytest ()
{
B::M();
}it goes "up" the hierarchy an calls the next found M(); in this case A::M() But in class D´s DoSomething where I get a ptr to C1, or C2 ... how can do it there?
Ah the joys of deep class hierarchy in C++ and OOP coding circa 1980-1990 :-) Probably too much danger in doing what should be done with that old code and using modern Composition over inheritance style. It isn't going to be fun and C++ is going to fight you all the way as it will try to discourage that style. Working with old code I feel your pain ... good luck.
In vino veritas