I hope the following snippet explains it all:
struct TBase {
virtual void f() {}
};
struct TDerived : public TBase {
TDerived() {
/* These are all fine either under GCC 4.4.3, ICC 12 and Comeau online: */
f();
this->f();
TBase::f();
this->TBase::f();
/* This one fails under Comeau: */
TBase::TBase();
/* While this one fails in every case: */
//this->TBase::TBase();
/* e.g. GCC says:
test.cpp: In constructor ‘TDerived::TDerived()’:
test.cpp:17: error: invalid use of ‘struct TBase’ */
}
void f() {}
};
The question is: why? (i.e. why is TBase::TBase() wrong, according to Comeau C++? Why is this->TBase::TBase() even wronger?)
Because you cannot call any constructor directly (§12.1 (2) ISO/IEC 14882:2003(E)). If you want to call a base class constructor, you will have to do so in the initializer list, i.e.:
The main reason for this is that by the time control reaches the first executable code line of your derived constructor, it is guaranteed that the base class object is already fully constructed (§12.6.2 (5) and (6) ISO/IEC 14882:2003(E)). Since constructors are commonly used for resource acquisition (i.e. RAII), it would be an error if it was allowed to “double”-construct an object.