我一直在审查一些看起来像这样的代码:
class A; // defined somewhere else, has both default constructor and A(int _int) defined
class B
{
public:
B(); // empty
A a;
};
int main()
{
B* b;
b = new B();
b->a(myInt); // here, calling the A(int _int) constructor,
//but default constructor should already have been called
}
Run Code Online (Sandbox Code Playgroud)
这有用吗?在调用默认值后调用特定的构造函数?
jal*_*alf 15
该代码不会调用构造函数.它叫A::operator()(int).
但是如果你在一个已经构造的对象上显式调用构造函数,那么你就可以很好地进入未定义的行为.它似乎在实践中起作用,但不能保证它会按预期执行.