1

I assume the answer is yes but I haven't explicitly found an assertion of this for every possible situation.

Given an intricate inheritance tree that probably would require step by step upcasting due to intermediate non virtual inheritance and also after multiple chains of upwards casting and downwards dynamic casting of pointers (always starting from the very same instance) the final address to the virtual base of that instance is guaranteed to be the same?

class A {};
class B : virtual public A {};
class C : virtual public A {};
class D : public B, public C {};

class E{};
class F{};

class G : public D, public E{};
class H : public D, public F{};
class I : public G, public H{};

...

class X: ... {} //Some inheritance that has A as virtual base

//We start with pointers from an X type instance:
X x;
X* p_i = &x;

//We always end with A* pointers following different paths:
A* a_i = (A*) ...

I guess I answered myself saying "the address to the virtual base of the same instance" should obviously be the same.

But if that is the case and extending the question, if we have obtained the same address (only through casting) but using different paths, could we end in a situation "similar" to this one where we get the same address but we are not guaranteed to end up with a defined behavior if we wanted to use the final pointer? Link to ccpreference where the following note is shown:

Note that two pointers that represent the same address may nonetheless have different values

struct C {
   int x, y;
} c;
 
int* px = &c.x;   // value of px is "pointer to c.x"
int* pxe= px + 1; // value of pxe is "pointer past the end of c.x"
int* py = &c.y;   // value of py is "pointer to c.y"
 
assert(pxe == py); // == tests if two pointers represent the same address
                   // may or may not fire
 
*pxe = 1; // undefined behavior even if the assertion does not fire
SaintJob 2.0
  • 554
  • 4
  • 21
  • 1
    Assuming there is only one subobject of type `A` in `X`, and assuming all the casts are legal, the final pointer will be the address of that single subobject. I don't see much similarity with the other case where one of the values was derived using pointer arithmetics on an entirely different, unrelated "subobject". – dxiv Dec 15 '20 at 01:23
  • @dxiv I just couldn't easily formulate the whole question (2 in 1). The similarity would come if there was some kind of pointer arithmetics involving some casting paths, incorrect ways of doing them leading to different results or the same address but the behavior of the second case. Anyways, you pretty much clarified it with your comment. – SaintJob 2.0 Dec 15 '20 at 02:03
  • 2
    **You should definitively not use C style cast if your code has multiple or virtual inheritance**. In many case, if you modify the hierarchy, C style cast would still compile even if the result is incorrect and it will lead to hard to find bugs and crashes. Always use `static_cast` or `dynamic_cast` to walk classes hierarchy. – Phil1970 Dec 15 '20 at 02:07

1 Answers1

1

The answer is yes. You should try to use C++ style code and not C style. When you use C style, you do not get all of the features you can use in c++ style for your code. That is why C++ exists, it is object-oriented. ;)

EDIT: C style cast* EDIT: You can use things like tutorialspoint for help on this stuff!

  • Have a great day!
Skyy Civil
  • 63
  • 8