Class Question

C

Charles Keepax

Sorry about the newbie question. If a class contains a pointer to some
data, and memory is allocated to that pointer in the constructor.
Naturally the destructor should free that memory, right? So since C++ is
by default call-by-value, if you pass that class as an arguement to a
function a copy of the class will be made however a copy of the data
isn't. Thus when the function reaches its end, the class destructor will
be called thus destroying the data. However the original object still
exists and when it trys to reference the data an error will result. So
finally, my question is appart from passing the class as a reference
arguement or pointer is there anything that can be done to prevent this?

Thanks,
Charles
 
R

Rolf Magnus

Charles said:
Sorry about the newbie question. If a class contains a pointer to some
data, and memory is allocated to that pointer in the constructor.
Naturally the destructor should free that memory, right?
Yes.

So since C++
is by default call-by-value, if you pass that class as an arguement to
a function a copy of the class will be made however a copy of the data
isn't.

If you call-by-value, the object is copied, but the data is not part of
the object. The pointer is, and so the pointer will be copied, but not
the data it points to.
Thus when the function reaches its end, the class destructor
will be called thus destroying the data. However the original object
still exists and when it trys to reference the data an error will
result.
Exactly.

So finally, my question is appart from passing the class as a
reference arguement or pointer is there anything that can be done to
prevent this?

Yes, follow the "Rule of Three": If your class needs one of a copy
constructor, assignment operator or destructor, it probably needs all
three of them.
IOW, just write your own copy constructor and assignment operator that
do the necessary allocation and copying of the data your pointer points
to. Alternatively, you could use reference counting, i.e. you count how
many objects have a pointer to the same data, and only deallocate the
memory when the last object is destroyed.
 
C

Charles Keepax

Thanks.

Charles

Rolf said:
Charles Keepax wrote:




If you call-by-value, the object is copied, but the data is not part of
the object. The pointer is, and so the pointer will be copied, but not
the data it points to.




Yes, follow the "Rule of Three": If your class needs one of a copy
constructor, assignment operator or destructor, it probably needs all
three of them.
IOW, just write your own copy constructor and assignment operator that
do the necessary allocation and copying of the data your pointer points
to. Alternatively, you could use reference counting, i.e. you count how
many objects have a pointer to the same data, and only deallocate the
memory when the last object is destroyed.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,126
Messages
2,570,753
Members
47,313
Latest member
SamualGriz

Latest Threads

Top