1

I just get the information from this cool blog that we can cast the derived object to inaccessible base class sub-object. On the other hand, the static_cast cannot be used to do that.

class Base {};
class Derived: private Base {};
void main()
{
    Derived d;
    Base* b = (Base*)&d;   // compile passed.
    Base* c = static_cast<Base*>(&d); // compile error.
}

Cast the object to private base sub-object will break the private keyword mean, it make the private object accessible to caller. I assume there is some reason why C++ standard explicit allow this, and the reason "compliant to C language" doesn't work to me because there doesn't have inheriate in C language even you can simulate it, which is unrelated to this problem.

Can anyone share some thought on this?

ZijingWu
  • 3,350
  • 3
  • 25
  • 40
  • Are you using exactly that example? That code can't compile because you are redefining `b`. – Manu343726 Sep 11 '13 at 10:22
  • C has inheritance `struct a {int foo}; struct derived {struct a i, int some, int more};` you just need to make sure your parent/the struct you derive from is the first struct member - then you are free to cast `struct derived *y = ...; struct a *x = (struct a*)y;` – drahnr Sep 11 '13 at 10:24
  • @drahnr That isn't inheritance. At best, it can be used to emulate inheritance, but no one in their right mind would claim `struct Point { int x; int y; };` inherits from `int` :) –  Sep 11 '13 at 10:29
  • 1
    Note that a C style cast does not "care" about inheriance. For all the language cares, you can do `int x; Base *b = (Base *)&x;` and the compiler will do it's best to make the address of `x` into a pointer to a `Base` object. It is the equivalent (in many cases) to a `reinterpret_cast` ratheer than a `static_cast`. And as such, the question is completely unrelated to inheritance. [Of course, my example may cause undefined behaviour, just like any other "wild" use of a C style cast). – Mats Petersson Sep 11 '13 at 10:30
  • @MatsPetersson A C style cast does care about inheritance, and a simple test with multiple inheritance can show this. If you have structs `A`, `B : A`, `C : A`, and `D : B, C`, a C style cast from `D *` to `A *` gives a compiler error. If you have structs `A`, `B`, `C : A` and `D : B, C`, a C style cast from `D *` to `A *` adjusts the pointer to point to the right subobject. Even if the inheritance is private. –  Sep 11 '13 at 11:02
  • @MatsPetersson: But in this case, the cast is equivalent to `static_cast`, but ignoring accessibility; unlike `reinterpret_cast`, you'll get the correct result even if there are multiple base classes. – Mike Seymour Sep 11 '13 at 11:08
  • The most likely reason is that this was the informal behaviour of early C++ compilers, and the standardisation process didn't want to break that behaviour. But I'm not familiar enough with the history of the language to say for sure. – Mike Seymour Sep 11 '13 at 11:13
  • @hvd just a matter of pov – drahnr Sep 11 '13 at 11:14
  • @Manu343726, example code updated. – ZijingWu Sep 11 '13 at 12:19

0 Answers0