views:

497

answers:

8

Hi,

Consider this simple situation:

A.h

class A {
public:
    virtual void a() = 0;
};

B.h

#include <iostream>

class B {
public:
    virtual void b() {std::cout << "b()." << std::endl;};
};

C.h

#include "A.h"
#include "B.h"

class C : public B, public A {
public:
    void a() {std::cout << "a() in C." << std::endl;};
};

int main() {
    B* b = new C();
    ((A*) b)->a();    // Output: b().

    A* a = new C();
    a->a();           // Output:: a() in C.

   return 0;
}

In other words:
- A is a pure virtual class.
- B is a class with no super class and one non-pure virtual function.
- C is a subclass of A and B and overrides A's pure virtual function.

What surprises me is the first output i.e.

((A*) b)->a();    // Output: b().

Although I call a() in the code, b() is invoked. My guess is that it is related to the fact that the variable b is a pointer to class B which is not a subclass of class A. But still the runtime type is a pointer to a C instance.

What is the exact C++ rule to explain this, from a Java point of view, weird behaviour?

+20  A: 

You are unconditionally casting b to an A* using a C-style cast. The Compiler doesn't stop you from doing this; you said it's an A* so it's an A*. So it treats the memory it points to like an instance of A. Since a() is the first method listed in A's vtable and b() is the first method listed in B's vtable, when you call a() on an object that is really a B, you get b().

You're getting lucky that the object layout is similar. This is not guaranteed to be the case.

First, you shouldn't use C-style casts. You should use C++ casting operators which have more safety (though you can still shoot yourself in the foot, so read the docs carefully).

Second, you shouldn't rely on this sort of behavior, unless you use dynamic_cast<>.

jeffamaphone
You can completely rely on cross-casting if `dynamic_cast` is used. It's 100% guaranteed to work or your money back.
coppro
@coppro: if you use dynamic_cast, yes. I was trying to emphasize not to rely on this working with the C-style cast. I updated my last statement to make that more clear.
jeffamaphone
+1 for getting lucky.
Martin York
+11  A: 

Don't use a C-style cast when casting across a multiple inheritance tree. If you use dynamic_cast instead you get the expected result:

B* b = new C();
dynamic_cast<A*>(b)->a();
sth
This should be a runtime error within the example given. Correct?
Eric Mickelsen
@tehMick, no it will be undefined behavior
Trent
@teh @Trent: Both of you are wrong. :P It works correctly, traversing the inheritance hierarchy.
GMan
I thought dynamic_cast was designed explicitly to provide run-time type checking. If it doesn't catch this, what good is it?
Eric Mickelsen
@GMan: What is the correct behavior when you call a() on an object of type B???
Eric Mickelsen
It does runtime type checking. And it sees that `b` is an instance of `C`, which does inherit from `A`.
Anon.
@jeffamaphone: Right, which should result in a runtime error once the call to a() on a null object is attempted.
Eric Mickelsen
Note that dynamic_cast<> only works if you have run-time type information enabled (/GR on the Windows compiler).
jeffamaphone
Oh, I see, sorry - it's an object of type C, not B. So, it should work. My mistake.
Eric Mickelsen
@tehMick, dynamic_cast knows that the object type is C. Since C derives from A, it is able to make the cast without run-time type checking finding anything to complain about.
Mark Ransom
@GMan, I was saying that the original code was undefined behavior, I mis-interpreted tehMick's comment.
Trent
+2  A: 

((A*) b) is an explicit c-style cast, which is allowed no matter what the types pointed to are. However, if you try to dereference this pointer, it will be either a runtime error or unpredictable behavior. This is an instance of the latter. The output you observed is by no means safe or guaranteed.

Eric Mickelsen
+5  A: 

You are starting with a B* and casting it to A*. Since the two are unrelated, you're delving into the sphere of undefined behavior.

Mark Ransom
A: 

I think you have a subtle bug in casting from B* to A*, and the behaviour is undefined. Avoid using C-style casts and prefer the C++ casts - in this case dynamic_cast. Due to the way your compiler has laid out the storage for the data types and vtable entries, you've ended up finding the address of a different function.

P-Nuts
+1  A: 

The following line is a reinterpret_cast, which points at the same memory but "pretends" it is a different kind of object:

((A*) b)->a();

What you really want is a dynamic_cast, which checks what kind of object b really is and adjust what location in memory to point to:

dynamic_cast<A*>(b)->a()

As jeffamaphone mentioned, the similar layout of the two classes is what causes the wrong function to be called.

Darryl
+1  A: 

There is almost never an occasion in C++ where using a C-style cast (or its C++ equivalent reinterpret_cast<>) is justified or required. Whenever you find yourself tempted to use one of the two, suspect your code and/or your design.

anon
+2  A: 

A and B are no related to each other by means of inheritance, which means that a pointer to B cannot be transformed into a pointer to A by means of either upcast or downcast.

Since A and B are two different bases of C, what you are trying to do here is called a cross-cast. The only cast in C++ language that can perform a cross-cast is dynamic_cast. This is what you have to use in this case in case you really need it (do you?)

B* b = new C(); 
A* a = dynamic_cast<A*>(b);
assert(a != NULL);
a->a();    
AndreyT