简体   繁体   中英

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 . 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)

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));

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?

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));

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.

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));

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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