A::A () : i(0),j(0) -->> here ? { -->> here ? //... -->> here ? }
解决方法
Member functions (including virtual member functions,10.3) can be called for an object under construction. Similarly,an object under construction can be the operand of the
typeid
operator (5.2.8) or of adynamic_cast
(5.2.7). However,if these operations are performed in a ctor-initializer (or in a function called directly or indirectly from a ctor-initializer) before all the mem-initializers for base classes have completed,the result of the operation is undefined.
实际上,在构建基础子对象期间,存在虚函数机制,但是它是为基类设置的.第[class.cdtor]节说:
Member functions,including virtual functions (10.3),can be called during construction or destruction (12.6.2). When a virtual function is called directly or indirectly from a constructor or from a destructor,including during the construction or destruction of the class’s non-static data members,and the object to which the call applies is the object (call it
x
) under construction or destruction,the function called is the final overrider
in the constructor’s or destructor’s class and not one overriding it in a more-derived class. If the virtual function call uses an explicit class member access (5.2.5) and the object expression refers to the complete object ofx
or one of that object’s base class subobjects but notx
or one of its base class subobjects,the behavior is undefined.