0

Consider the following code:

struct A { int m; };
struct B : A { void proc () { /* ... */ };
struct C : A { void proc () { /* ... */ };

A a;
B * b = (B *) &a;
C * c = (C *) &a;
b->proc ();
c->proc ();

Is that legal and well-defined?

Pro: A is standard layout and B and C do not contain any data members.

Contra: a is neither of B nor of C type.

JohnB
  • 13,315
  • 4
  • 38
  • 65
  • Probably unnecessary. If `B` and `C` have no state, then you should get the same behaviour from `B().proc();`. – Kerrek SB Dec 25 '13 at 20:32

1 Answers1

1

Formal: not valid, not well-defined. In-practice: not smart to do.

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
  • The original question, reworded: "If I kick this hornet's nest that I didn't see any hornets around, will I get stung?" :-) – Joe Z Dec 25 '13 at 21:37