使用 std::make_shared 抽象类实例化时出错 [英] error using std::make_shared abstract class instantiation

查看:116
本文介绍了使用 std::make_shared 抽象类实例化时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将省略相当多的代码,因为这些是一些相当大的对象,我的问题实际上只涉及 std::make_shared 的操作.我在命名空间 SYNC 中有一个名为 D3D11Shader 的对象.这有一个静态函数,

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 . I have an object in namespace SYNC called called D3D11Shader. This has a static function called,

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

它将接受一个字符串索引并返回一个指向从 SYNC::D3D11Shader 派生的着色器实例的指针.有一次,我开始使用智能指针在包含所有这些着色器的向量中自动释放这些指针.但是,当我这样做时,

which will take a string index and return a pointer to an instance of a shader that is derived from 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));

编译器错误说我试图在这一行中实例化 D3D11Shader 的一个实例,它是一个抽象类.我认为 make_shared 所做的一切都是返回 std::shared_ptr 的一个实例.CreateInstance 函数从不尝试创建此类的实例,只是派生和实现它的对象.在使用这个函数和智能指针之前,我没有收到这个错误.有人知道这里发生了什么吗?

the compiler errors saying that i am trying to instanciate an instance of D3D11Shader in this line which is an abstract class. I thought all make_shared did was return an instance of std::shared_ptr. The CreateInstance function never tries to make an instance of this class, just objects that derive and implement it. I was not getting this error before using this function and the smart pointers. Does anyone know what's going on here?

推荐答案

如果不能使用 shared_ptr 的构造函数,请使用它的 reset 成员函数赋予它一个新对象的所有权:

If you can't use the constructor of shared_ptr, use its reset member function to give it ownership of a new object:

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

make_shared 不适合这种情况的原因是它构造了一个新的 T,将它的参数转发给它的构造函数.不过,您已经构建了一个对象,因此您只想将所有权授予共享指针.

The reason make_shared<T> is not appropriate for this situation is because it constructs a new T, forwarding its arguments to its constructor. You've already constructed an object, though, so you just want to give ownership to your shared pointer.

我强烈建议不要从 CreateShader 返回原始指针.您依赖 CreateShader 的调用者知道是将其包装在智能指针中还是对其调用 delete.你最好直接返回一个 unique_ptr ,将所有权传递给客户端,然后他们可以根据需要制作一个 shared_ptr .请参阅以下内容:

I highly recommend not returning a raw pointer from CreateShader though. You're relying on the caller of CreateShader to know to either wrap it up in a smart pointer or call delete on it. 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. 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));

或者干脆:

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

如果您要使用智能指针,请使用它们.:)

If you're going to use smart pointers, use them. :)

这篇关于使用 std::make_shared 抽象类实例化时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆