classes, parameters and destructors
How do you pass a class to a function and not set off the destructor when the function is complete?
I am passing a homemade string class with a destructor that destroys the string pointer. So it seems to me that it is creating a new class and then destroying the original when it goes out of scope.
Allocate it on the heap, and pass a pointer to it. Or pass the object by reference (depending on usage).
quote:Original post by squicklid
I am passing a homemade string class with a destructor that destroys the string pointer. So it seems to me that it is creating a new class and then destroying the original when it goes out of scope.
It sounds like you need to define a copy constructor and an assignment operator. The "rule of three" states that if you need a destructor, an assignment operator, or a copy constructor, you probably need all three.
Of course, passing the string by reference will be more efficient.
EDIT: Oh, and SiCrane is referring to allocation through the use of the
new
keyword."Sneftel is correct, if rather vulgar." --Flarelocke
[edited by - sneftel on April 15, 2004 4:04:17 AM]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement