Topic: Weird cast question.


Author: "William E. Aitken" <aitken@halcyon.com>
Date: 1996/06/30
Raw View
Suppose we have an inheritence heirarchy like the following (note the
base class constructors that take derived classes by value.

struct C;
struct B;
struct A {
 A(const A &);
 A();
 A(B);
};
struct B : A {
        B(const B&);
        B();
        B(C);
};
struct C : B {
 C();
 C(const C&);
};

Suppose that c is a variable of type C.

What constructors get called to implement the cast expression (A)c.

In particular, (1) is it legal just to call A::A(A& ra) by binding the A
subobject of c to ra?   Or do we (2) have to first call C::C(C& rc) by binding
c to rc to generate a temporary tc, then call B::B(C) on tc to generate
a temporary tb, and then call A::A(B) on tb to generate our result?
Or (3) is this code illegal for some reason---ambiguity perhaps, or some
obscure restriction on the formal types of constructors?

If (2), can we optimize this by simply calling the A constructor called by
C::C(C &) --- assuming we know what that will be, A::A(), by default.

Is the answer any different if any of the copy constructors are automatically
generated, or lack the const?

--- Bill.
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]