R
Risto Lankinen
Timothy Madden said:Hello
Is my program legal and well formed:
class Data;
class SMax
{
class Data &Buffer;
public:
SMax(class Data &buffer)
:Buffer(buffer) { }
~SMax()
{ delete &Buffer; }
}
?
That is, can I delete the address within a reference ?
Purely technically, yes.
However, in practice you are placing a lot of constraints
to the user of your class. For instance, whoever instantiates
class SMax will have to provide a [reference to a] deletable
Data object, requiring all buffers to be dynamically allocated.
Also, if the caller is responsible for the lifetime management
of the buffer, you run the risk that a buffer might be deleted
while some SMax object still holds a refeence to it.
This is a lot of responsibility to be placed on the user of
your class, so although its design is technically correct, it
could be improved to be more robust and user friendly.
Improvement ideas (from most to least recommended):
- Let the class itself both create and delete the buffer
- Make class Data reference counted instead
- Let the caller both create and delete the buffer
Cheers!
- Risto -