简体   繁体   English

使用std :: make_shared抽象类实例化时出错

[英]error using std::make_shared abstract class instantiation

I'm going to omit quite bit of code because these are some rather large objects, and my question really just concerns the operation of std::make_shared . 我将省略相当多的代码,因为这些是一些相当大的对象,我的问题实际上只涉及std :: make_shared的操作。 I have an object in namespace SYNC called called D3D11Shader. 我在命名空间SYNC中有一个名为D3D11Shader的对象。 This has a static function called, 这有一个名为的静态函数,

SYNC::D3D11Shader * SYNC::D3D11Shader::CreateInstance(const std::string & s)

which will take a string index and return a pointer to an instance of a shader that is derived from SYNC::D3D11Shader. 它将获取一个字符串索引并返回一个指向从SYNC :: D3D11Shader派生的着色器实例的指针。 At one point i started using smart pointers to automate deallocation of these within the vector that held all these shaders. 有一次,我开始使用智能指针在包含所有这些着色器的向量中自动重新分配这些指针。 However, when i go to do this, 但是,当我去做这个,

 std::shared_ptr<SYNC::D3D11Shader> shaderPtr;
 // ... verification of index and other initialization happens here
 // so i am unable to initialize it in it's constructor
 shaderPtr = std::make_shared<SYNC::D3D11Shader>(SYNC::D3D11Shader::CreateShader(shaderName));

the compiler errors saying that i am trying to instanciate an instance of D3D11Shader in this line which is an abstract class. 编译器错误说我试图在这一行中实现D3D11Shader的实例,这是一个抽象类。 I thought all make_shared did was return an instance of std::shared_ptr. 我以为所有make_shared都返回了一个std :: shared_ptr的实例。 The CreateInstance function never tries to make an instance of this class, just objects that derive and implement it. CreateInstance函数从不尝试创建此类的实例,只是尝试派生和实现它的对象。 I was not getting this error before using this function and the smart pointers. 在使用此函数和智能指针之前,我没有收到此错误。 Does anyone know what's going on here? 有谁知道这里发生了什么?

If you can't use the constructor of shared_ptr , use its reset member function to give it ownership of a new object: 如果您不能使用shared_ptr的构造函数,请使用其reset成员函数为其赋予新对象的所有权:

std::shared_ptr<SYNC::D3D11Shader> shaderPtr;
shaderPtr.reset(SYNC::D3D11Shader::CreateShader(shaderName));

The reason make_shared<T> is not appropriate for this situation is because it constructs a new T , forwarding its arguments to its constructor. make_shared<T>不适合这种情况的原因是因为它构造了一个新的T ,将其参数转发给它的构造函数。 You've already constructed an object, though, so you just want to give ownership to your shared pointer. 但是,您已经构建了一个对象,因此您只想为共享指针赋予所有权。

I highly recommend not returning a raw pointer from CreateShader though. 我强烈建议不要从CreateShader返回原始指针。 You're relying on the caller of CreateShader to know to either wrap it up in a smart pointer or call delete on it. 您依赖于CreateShader的调用者知道要么将其包装在智能指针中还是在其上调用delete You'd be better off returning a unique_ptr directly, to pass ownership to the client, and then they can make a shared_ptr out of it if they like. 你最好直接返回unique_ptr ,将所有权传递给客户端,然后如果他们愿意,他们可以从中创建一个shared_ptr See the following: 请参阅以下内容:

std::unique_ptr<SYNC::D3D11Shader> uniquePtr(SYNC::D3D11Shader::CreateShader(shaderName));
// If you want a shared_ptr:
std::shared_ptr<SYNC::D3D11Shader> sharedPtr(std::move(uniquePtr));

Or simply: 或者干脆:

std::shared_ptr<SYNC::D3D11Shader> sharedPtr = SYNC::D3D11Shader::CreateShader(shaderName);

If you're going to use smart pointers, use them. 如果您要使用智能指针,请使用它们。 :) :)

Easy enough: 很容易:

shaderPtr.reset(SYNC::D3D11Shader::CreateShader(shaderName));

You can see the different variants of the reset member function here . 您可以在此处查看 reset成员函数的不同变体。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM