C ++如何从class创建一个std::unique_ptr,它在构造函数上接受参数
C++ How to create a std::unique_ptr from a class that takes parameters on constructor
我需要从 class 创建一个 std::unique_ptr
,它有一个带一个参数的构造函数。我找不到有关如何操作的参考资料。这是无法编译的代码示例:
#include <iostream>
#include <string>
#include <sstream>
#include <memory>
class MyClass {
public:
MyClass(std::string name);
virtual ~MyClass();
private:
std::string myName;
};
MyClass::MyClass(std::string name) : myName(name) {}
MyClass::~MyClass() {}
class OtherClass {
public:
OtherClass();
virtual ~OtherClass();
void MyFunction(std::string data);
std::unique_ptr<MyClass> theClassPtr;
};
OtherClass::OtherClass() {}
OtherClass::~OtherClass() {}
void OtherClass::MyFunction(std::string data)
{
std::unique_ptr<MyClass> test(data); <---------- PROBLEM HERE!
theClassPtr = std::move(test);
}
int main()
{
OtherClass test;
test.MyFunction("This is a test");
}
这些错误与我初始化 std::unique_ptr
的方式有关,在我的代码中指出。
原代码和错误可以找到here。
感谢您帮我解决这个问题。
#include <memory>
...
int main()
{
std::string testString{ "Testing 1...2....3" };
auto test = std::make_unique<MyClass>( testString );
return 0;
}
你可以这样做:
std::unique_ptr<MyClass> test(new MyClass(data));
或者如果您有 C++14
auto test = std::make_unique<MyClass>(data);
但是:
在提供的示例中,无需创建临时变量,您只需使用 class 成员的 reset
方法即可:
theClassPtr.reset(new MyClass(data));
这基本上是一个疏忽。你需要这个:
#include <memory>
namespace std
{
template <class T, class... Args>
std::unique_ptr <T> make_unique (Args&&... args)
{
return std::unique_ptr <T> (new T (std::forward <Args> (args)...));
}
}
C++14 自带 std::make_unique
。它在 C++11 中被省略了。
自己写很简单:
namespace notstd{
template<class T,class...Args>
std::unique_ptr<T> make_unique(Args&&...args){
return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
}
}
现在这样使用:
auto foo = notstd::make_unique<MyClass>(string);
将为您制作独一无二的ptr。
这种模式有几个优点。首先,它从 "user code" 中删除了未与 delete
配对的 new
,这让我很高兴。
其次,如果你调用一个函数,它接受 2 个 unque 指针,上面的方法可以避免在抛出异常的情况下发生泄漏。
我们把它放在 notstd
中,因为在 std
中注入新函数在标准中是非法的(不需要诊断)。
我需要从 class 创建一个 std::unique_ptr
,它有一个带一个参数的构造函数。我找不到有关如何操作的参考资料。这是无法编译的代码示例:
#include <iostream>
#include <string>
#include <sstream>
#include <memory>
class MyClass {
public:
MyClass(std::string name);
virtual ~MyClass();
private:
std::string myName;
};
MyClass::MyClass(std::string name) : myName(name) {}
MyClass::~MyClass() {}
class OtherClass {
public:
OtherClass();
virtual ~OtherClass();
void MyFunction(std::string data);
std::unique_ptr<MyClass> theClassPtr;
};
OtherClass::OtherClass() {}
OtherClass::~OtherClass() {}
void OtherClass::MyFunction(std::string data)
{
std::unique_ptr<MyClass> test(data); <---------- PROBLEM HERE!
theClassPtr = std::move(test);
}
int main()
{
OtherClass test;
test.MyFunction("This is a test");
}
这些错误与我初始化 std::unique_ptr
的方式有关,在我的代码中指出。
原代码和错误可以找到here。
感谢您帮我解决这个问题。
#include <memory>
...
int main()
{
std::string testString{ "Testing 1...2....3" };
auto test = std::make_unique<MyClass>( testString );
return 0;
}
你可以这样做:
std::unique_ptr<MyClass> test(new MyClass(data));
或者如果您有 C++14
auto test = std::make_unique<MyClass>(data);
但是:
在提供的示例中,无需创建临时变量,您只需使用 class 成员的 reset
方法即可:
theClassPtr.reset(new MyClass(data));
这基本上是一个疏忽。你需要这个:
#include <memory>
namespace std
{
template <class T, class... Args>
std::unique_ptr <T> make_unique (Args&&... args)
{
return std::unique_ptr <T> (new T (std::forward <Args> (args)...));
}
}
C++14 自带 std::make_unique
。它在 C++11 中被省略了。
自己写很简单:
namespace notstd{
template<class T,class...Args>
std::unique_ptr<T> make_unique(Args&&...args){
return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
}
}
现在这样使用:
auto foo = notstd::make_unique<MyClass>(string);
将为您制作独一无二的ptr。
这种模式有几个优点。首先,它从 "user code" 中删除了未与 delete
配对的 new
,这让我很高兴。
其次,如果你调用一个函数,它接受 2 个 unque 指针,上面的方法可以避免在抛出异常的情况下发生泄漏。
我们把它放在 notstd
中,因为在 std
中注入新函数在标准中是非法的(不需要诊断)。