I'm trying to build a struct with a constructor and destructor in C++ (is that a bad thing to do? Should I use a class instead?), because I would hate to write unnecessary 10 lines of delete struct.member every time the members should be deallocated
The members of the struct are mostly pointers to other types.
However, apart from the option of the member pointer being a unique pointer, I also want to be able to assign a copy of another pointer to a member pointer in the struct. Consequently, if I try to deallocate that memory inside the destructor of the struct, it could have already been deallocated, causing a crash. (especially if the pointer copied was from another object of the same struct)
ptr to ptr-types instead of ptr-types in the struct wouldn't solve the problem either (I think), since I also want to allow the member to be able to be a unique pointer to an object.
A possible solution that I've thought of, is to have both a ptr-to-ptr and a ptr to be pointed to by the ptr-to-ptr. But that would just be rather inefficient.
What would my best solution be?
I hope (but doubt) my question is clear enough.
Here's some example code that might help.
struct GraphicsDesc
{
public:
ID3D11VertexShader* pSolidColorVS;
ID3D11PixelShader* pSolidColorPS;
ID3D11InputLayout* pInputLayout;
ID3D11ShaderResourceView* pColorMap;
ID3D11SamplerState* pSampler;
ID3D11BlendState* pBlendState;
ID3D11Buffer* pVertexBuffer;
UINT VertexSize;
D3D_PRIMITIVE_TOPOLOGY Topology;
GraphicsDesc()
{
pSolidColorVS = nullptr;
pSolidColorPS = nullptr;
pInputLayout = nullptr;
pColorMap = nullptr;
pSampler = nullptr;
pBlendState = nullptr;
pVertexBuffer = nullptr;
VertexSize = 0;
Topology = D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST;
}
virtual ~GraphicsDesc()
{
if (pVertexBuffer) {
pVertexBuffer->Release();
}
if (pBlendState) {
pBlendState->Release();
}
if (pSampler) {
pSampler->Release();
}
if (pColorMap) {
pColorMap->Release();
}
if (pInputLayout) {
pInputLayout->Release();
}
if (pSolidColorPS) {
pSolidColorPS->Release();
}
if (pSolidColorVS) {
pSolidColorVS->Release();
}
}
};