|There is no unique answer, since it mostly depend on the semantics your class intended to be used and on the semantic of the resource (memory is a particular cas of resource, but having an
HANDLE instead of an
int* doesn't change the nature of your question) is planned to be represented.
The first thing to decide is what should happen when an instance of your class is assigned to another. Should both the classes refer to the same resource (that is: whatever modification required from both will act on the same object) or should them refer to different resources (that is: you need shallow or deep copy)?
Or should one instance represent one resource, hence no copy/assignment can be done and your classes must always pass by reference?)
The second thing to decide is the kind of "ownership" your class has on the resource: Does it own it (has the right to destroy it?) Is it an exclusive owner? Can it pass the ownership to somebody else? Should it share the ownership with other copies?
The following table may help:
| ||no copy ||shallow |
|no ownership ||use dumb pointers and disable copy and assing ||use dumb pointer and don't care ||NO SENSE |
|exclusive ||delete on destruction, disable copy/assign ||NO SENSE ||implement copy and assign to create resource copies |
|pass-through ||NO SENSE ||use auto_ptr or implement equivalent semantic (transef |
the pointer to the copy and make the original pointer null)
|NO SENSE |
|sharing ||NO SENSE ||use reference counting or a reference counting capable |
pointer (like shared_ptr)
|NO SENSE |
A third thing to decide is how your class acquire the resource: does it create it? Does it get it from a manages? is it created outside and passed to be handled?
As you see, there are many choices, some optimal in certain cases, some optimal in other.
2 bugs found.
> recompile ...
65534 bugs found.