I know that this question must have been covered endlessly many times, but I have looked for the previous questions and nothing seems to pop.
This is about inheritance and virtual functions in C ++. I have a problem with calling virtual functions in subclasses from a superclass.
Let me give you an example. Start with three classes that inherit from each other.
class A {
void foo() { bar() }
virtual void bar() { }
};
class B : public A {
virtual void bar() { }
};
class C : public B {
virtual void bar() {
};
Now I want the variable to be declared as B *, but to be created as C *.
B* myObject = new C();
myObject->foo();
When I do this and call foo () on myObject, then A :: foo () calls bar (). But only B :: bar () is called, not C :: Bar () - which is actually myObject, although it is declared as B, which again affects the fact that "// does nothing" is not executed.
How do I tell A :: foo () that it should look at the lowest implementation?
?
//
EDIT:
C:: Foo . Foo A, , . , A: Foo Bar(). B: Bar, C:: Bar.
, , void * A.
:
void A:Foo(void *a) {
A* tmpA = static_cast<A*> (a);
tmpA->bar();
}
, tmpA A. - , B *, B:: Bar, tmpA C *, C:: Bar.