Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

squicklid

classes, parameters and destructors

This topic is 5206 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
Advertisement
Allocate it on the heap, and pass a pointer to it. Or pass the object by reference (depending on usage).

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!