Question

In C struct's, I'm guaranteed that:

struct Foo { ... };
struct Bar {
  Foo foo;
  ...
}
Bar bar;
assert(&bar == &(bar.foo));

Now, in C++, if I have:

class Foo { ... };
class Bar: public Foo, public Other crap ... {
  ...
}

Bar bar;
assert(&bar == (Foo*) (&bar)); // is this guaranteed?

If so, can you give me a reference (like "The C++ Programming Language, page xyz")?

Thanks!

Was it helpful?

Solution

There is no guarantee. From the C++03 standard (10/3, class.derived):

The order in which the base class subobjects are allocated in the most derived object (1.8) is unspecified.

OTHER TIPS

Even though layout for base classes is not guaranteed in the way you seem to have been thinking (and even though there are more guarantees for members), this is guaranteed:

Bar bar;
assert(&bar == (Foo*) (&bar));

Because the cast uses a static_cast (per 5.4) which will convert &bar correctly, and the comparison between pointer-to-base and pointer-to-derived will convert similarly.

This, however, would not be guaranteed:

Bar bar;
void* p1 = &bar;
void* p2 = (Foo*)&bar;
assert(p1 == p2); // not guaranteed

I don't imagine it would be, I don't know why it would need to be.

I have a related question.

if you have diamond inheritance:

class Base(){ virtual void AFunc(){}  };

class A:Base{};
class B:Base{void AFunc(){} }

class Test:A,B{};


Base * b = (Base*)new Test;

b->AFunc();

lets assume that memory structure is Base:A:B:Test,

note at the point of calling, all the compiler knows is the address of the beginning of the object, and AFunc is expecting to be relative to the beginning of the B object, these two addresses won't be the same! so how does it work?

like if b was of type B, the two addresses would be the same...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top