On Jun 26, 2:20 pm, "Fraser Ross" <z...@zzzzzz.com> wrote:
> >"James Kanze"
> >On Jun 25, 7:25 pm, "Fraser Ross"
> >> class base {
> >> public:
> >> base(void *) {}
> >> };
> >> class der : base {
> >> public:
> >> der() :base(this) {}
> >> };
> >> Does this have any defined value when used in a base class
> >> initialiser?
> >Yes, but it might not be what is wanted. The implicit
> >conversion to void* is very dangerous here; not only can base
> >not use the pointer in its constructor, it must convert it to
> >der* in order to use it later.
> It could also be converted later to base*.
If you don't mind undefined behavior. The only legal use of a
void* is converting it back to the original type. Here, the
original type is der*, not base*.
> I think my real code might violate 12.7/3. Where it says
> "might use path E* -> D*-> A*" is it implying that conversions
> from E* to C* and D* are equally good because they are
> directly inherited base classes? If I remove C, B and X from
> the example and force the conversions of E* -> D*-> A* the
> Comeau compiler gives no diagnostics but the standard is
> saying its undefined behaviour.
You'll have to show use your real code. My comments concerning
undefined behavior mainly concerned the passage through a void*.
The examples in 12.7/3 don't have any void*. §12.7/2 does
guarantee that you could pass your this to a base*, but it
doesn't say anything about a void*.
> struct A { };
> struct D : virtual A {
> D(A*);
> };
> struct E : D {
> E() : D(this) {}// undefined: upcast from E* to A* might use path E* !
> D* ! A*
> };
I'm not sure vis-a-vis the standard, but I do know that this
fails with certain compilers.
Curiously, you can call member functions of A in the same
context (and calling such functions requires the same
conversion). And the member function can return its this
pointer.
--
James Kanze (GABI Software) email:
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
|