从 class 成员到 return 智能指针的正确方法?
The right way to return a smart pointer from a class member?
我正在尝试在 person class 中编写单例模式,这使我能够为 class 创建一个实例,并且我可以在我的程序中的任何地方使用它。
以下是class:
// The declaration
class Person {
static unique_ptr<Person> instance;
Person() = default;
Person(Person&) = delete;
Person& operator=(const Person&) = delete;
~Person() = default;
public:
static unique_ptr<Person> getInstance();
};
// The implementation
unique_ptr<Person> instance = NULL;
unique_ptr<Person> Person::getInstance() {
if (instance == NULL) {
instance = unique_ptr<Person>(new Person());
}
return instance;
}
但是它给了我这个错误的问题:Error C2280 'std::unique_ptr<Person,std::default_delete<_Ty>>::unique_ptr(const std::unique_ptr<_Ty,std::default_delete<_Ty>> &)': attempting to reference a deleted function
不幸的是,我不明白那个问题,我不知道如何解决?
std::unique_ptr
的复制构造函数被隐式删除,因为它有一个显式定义的移动构造函数。
来自C++11 Standard, 12.8 Copying and moving class objects:
7 If the class definition does not explicitly declare a copy constructor, one is declared implicitly. If the class definition declares a move constructor or move assignment operator, the implicitly declared copy constructor is defined as deleted; otherwise, it is defined as defaulted ([dcl.fct.def]).
您可以通过以下方式解决您的问题:
返回对 Person
的引用。
static Person& getInstance();
Person& Person::getInstance()
{
static Person p;
return p;
}
返回 shared_ptr<Person>
.
static std::shared_ptr<Person> getInstance();
std::shared_ptr<Person> Person::getInstance()
{
static std::shared_ptr<Person> p(new Person);
return p;
}
我推荐第一个解决方案,因为它更简单。
PS 请注意,它们都不需要使用 static
成员变量 instance
.
我正在尝试在 person class 中编写单例模式,这使我能够为 class 创建一个实例,并且我可以在我的程序中的任何地方使用它。
以下是class:
// The declaration
class Person {
static unique_ptr<Person> instance;
Person() = default;
Person(Person&) = delete;
Person& operator=(const Person&) = delete;
~Person() = default;
public:
static unique_ptr<Person> getInstance();
};
// The implementation
unique_ptr<Person> instance = NULL;
unique_ptr<Person> Person::getInstance() {
if (instance == NULL) {
instance = unique_ptr<Person>(new Person());
}
return instance;
}
但是它给了我这个错误的问题:Error C2280 'std::unique_ptr<Person,std::default_delete<_Ty>>::unique_ptr(const std::unique_ptr<_Ty,std::default_delete<_Ty>> &)': attempting to reference a deleted function
不幸的是,我不明白那个问题,我不知道如何解决?
std::unique_ptr
的复制构造函数被隐式删除,因为它有一个显式定义的移动构造函数。
来自C++11 Standard, 12.8 Copying and moving class objects:
7 If the class definition does not explicitly declare a copy constructor, one is declared implicitly. If the class definition declares a move constructor or move assignment operator, the implicitly declared copy constructor is defined as deleted; otherwise, it is defined as defaulted ([dcl.fct.def]).
您可以通过以下方式解决您的问题:
返回对
Person
的引用。static Person& getInstance(); Person& Person::getInstance() { static Person p; return p; }
返回
shared_ptr<Person>
.static std::shared_ptr<Person> getInstance(); std::shared_ptr<Person> Person::getInstance() { static std::shared_ptr<Person> p(new Person); return p; }
我推荐第一个解决方案,因为它更简单。
PS 请注意,它们都不需要使用 static
成员变量 instance
.