[gnu.g++.help] 'this' in member initialization

chatty%FRLRI61.BITNET@CUNYVM.CUNY.EDU (02/08/91)

// the following program is accepted by both g++ and cfront 2.0.
// It leads to the suprising thing that a virtual method of class B is
// called before the body of the constructor is entered.

// I think ARM, p47, s5.1,  forbids the use of 'this' in member
// initialization.

// any comments?

extern "C" void printf (const char*, ...);

class A {
public:
 A () {}
 virtual void foo () {printf ("A::foo\n");}
};

class Z {
public:
 Z (A* a) {a->foo ();}
};

class B : public A {
public:
 Z z;
 B () : A (), z (this) {printf ("B::B\n");} // legal?!
 void foo () {printf ("B::foo\n");}
};

main ()
{
 B b;
}

// % a.out
// B::foo
// B::B



Stephane Chatty         chatty@lri.lri.fr, chatty@frlri61.bitnet