I understand this should have been covered endless of occasions, but I have looked the prior questions, and absolutely nothing appears to pop.

It comes down to inheritance and virtual functions i C++. I have trouble with calling virtual functions in subclasses in the superclass.

Allow me to give a good example. Oncoming of with three classes, which inherit from one another.

class A {

    void foo() { bar() }
    virtual void bar() { }

};

class B : public A {

    virtual void bar() { }

};

class C : public B {

    virtual void bar() { // do something }

};

Now I wanna possess a variable declared as B* but instantiated as C*.

B* myObject = new C();
myObject->foo();

After I do that, and call foo() on myObject, a::foo() is asking bar(). Only B::bar() is known as, not C::Bar() - which the truth is myObject is, despite the fact that it's declared as B, which again affects that "// do nothing at allInch does not get performed.

How do you tell A::foo(), that it must take a look at cheapest implementation?

Is sensible?

// Trenskow

EDIT:

C::Foo isn't the problem. Foo has been contacted class A, as it is the only real place it's implemented. The issue arises, whenever a:Foo calls Bar(). Then B:Bar is known as and never C::Bar.

Maybe the issue is, that during my implementation, I only obtain a void* pointer towards the object inside a.

Such as this:

void A:Foo(void *a) {

    A* tmpA = static_cast<A*> (a);
    tmpA->bar();

}

The compiler thinks, that tmpA is definitely an A. But in some way it handles to find it's a B*, and calls B::Bar, much more fact tmpA is really a C* and it ought to be calling C::Bar.

The next prints "A::foo C::bar" not surprisingly. Are you currently getting different things? B::bar isn't known as because C may be the actual runtime kind of the item. In C::bar, you can call B::bar clearly with the addition of B::bar(); to the body.

#include <iostream>
using namespace std;

class A {
public:
    void foo() { cout << "A::foo "; bar(); }
    virtual void bar() { }
};

class B : public A {
public:
    virtual void bar() { cout << "B::bar" << endl; }
};

class C : public B {
public:
    virtual void bar() { cout << "C::bar" << endl; }
};

int main()
{
    B* c = new C();
    c->foo();
    return 0;
}
void A:Foo(void *a) {

    A* tmpA = static_cast<A*> (a);
    tmpA->bar();

}

This really is undefined behavior. You can't cast a b -- to some void*, then cast that void* to a b-. If you would like results correctly, you need to ditch the void*. Alternatively, you could attempt dynamic_cast.

Presuming you mistyped your last block of code and also the names match:

B* variable = new C();
variable->foo();

Then your C::Foo technique is being known as or else you are utilizing a terribly bad compiler.

(This assumes you don't really possess a compiler error in C::Foo, which the comment is really something similar to std::cout << "Hi mom!" << std::endl;)

Not mean:

B* myObject = new C();
myObject->foo(); // not variable->foo()

class A
{
public:
    void foo() { bar(); }
    virtual void bar() { std::cout << "A"; };
};

class B : public A
{
public:
    virtual void bar() { std::cout << "B";};
};

class C : public B
{
public:
    virtual void bar() { std::cout << "C"; }
};

This prints 'C' not surprisingly.

I do not follow. You are saying

Only B::bar() is known as, not C::Bar()

No. You invoked the constructor of sophistication C, meaning the vtable makes bar() indicate C::bar(), so calling foo() within this situation would go right to C::bar().

If you wish to pressure A::foo() to clearly only call A's implementation, it can be done just by writing

void foo() { A::bar(); }

Exactly what are you attempting to do?

what compiler are you currently using? Visual Studio (IIRC) normally has the runtime type information switched off automatically so perhaps its just something simple as that?