c++: 在堆栈中实例化 class 的正确方法是什么
c++: what is correct way of instantiate a class in stack
class A{...};
int main(){
A a; //step 1
A a();//step 2
}
同时我尝试了以上2个案例
在步骤 1 中调用构造函数
在步骤 2 中未调用构造函数。
哪个有效。
这是非常基本的,但请解释第 1 步或第 2 步是否适合在堆栈中分配。
查看 C++ 常见问题解答 Is there any difference between List x; and List x();?
class A{...};
int main(){
A a; //step 1
A a();//step 2
}
同时我尝试了以上2个案例 在步骤 1 中调用构造函数 在步骤 2 中未调用构造函数。
哪个有效。 这是非常基本的,但请解释第 1 步或第 2 步是否适合在堆栈中分配。
查看 C++ 常见问题解答 Is there any difference between List x; and List x();?