How to properly create a copy constructor containing a SDL_Texture?

So I have created a Texture wrapper class for SDL_Texture (based off of Lazy Foo’s tutorials) and it has come to my attention that I really should have an explicit copy constructor and copy assignment operator to go with my destructor. My object attributes consist of a number of ints and a SDL_Texture pointer. My problem comes up when I try to properly copy the SDL_Texture struct in my copy constructor. I don’t want to just copy the pointer address, otherwise the compiler’s implicit copy constructor would do the job just fine. However as far as I can find, the SDL_Texture struct was made with the intention of not being copied, you can’t malloc(sizeof(SDL_Texture)) and there doesn’t seem to be any SDL function to copy a texture for you.

See:

Note that I don’t want to actually copy the texture in vram, that dosen’t make sense, I just want to copy the SDL_Texture struct contents which interacts with the graphics driver so that if I load a new texture to it, it wont overwrite anything for another object that was using that same texture.

So how exactly should I go about creating my copy constructor? Is there a proper way of doing this, am I just way off base and misunderstanding something, or is this one of the outlier cases where I need a destructor but not a copy constructor? Any help would be appreciated, thanks!

This question was prompted by my other question:

Your copy constructor should handle the SDL_Texture* in the same way it would handle any “raw” pointer.
Except that, as you said, you cannot copy the data of SDL_Texture. This solution only make sense for simple object anyway.

It depends on the behavior you want, but here some solutions:

  • No copy = no problem
    Texture(Texture& that) = delete;
    Texture& operator=(Texture& that) = delete;

  • You could use “smart” pointer (unique_ptr, shared_ptr, …): http://en.cppreference.com/w/cpp/memory

  • Use reference counting.

  • Pass the SDL_Texture* to the ‘copied to’ object. With this solution, only one Texture object has the SDL_Texture* and the ‘copied from’ object has no SDL_Texture*.