【发布时间】:2015-06-05 13:48:10
【问题描述】:
这个问题是关于我的程序的。我之前使用指针使用手动管理,现在我正试图转向智能指针(出于所有充分的理由)。
在普通指针中,使用 new 关键字很容易调用类的构造函数。就像在下面的程序中一样:
Button * startButton;
startButton = new Button(int buttonID, std::string buttonName);
使用智能指针时,调用类的构造函数的另一种方法是什么。我在下面所做的会出错:
std::unique_ptr<Button> startButton;
startButton = std::unique_ptr<Button>(1, "StartButton"); // Error
我得到的错误如下:
Error 2 error C2661: 'std::unique_ptr<Button,std::default_delete<_Ty>>::unique_ptr' : no overloaded function takes 2 arguments
【问题讨论】:
标签: c++ constructor smart-pointers