Advertisement Jump to content
Sign in to follow this  
AcidZombie24

constructor/reconstruct

This topic is 3993 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

I was wondering, is there an easy way to reconstruct a class? i am currently doing delete p; p = new theClass(newConstructor); i rather do something like reconstruct (p, newConstructor);

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by AcidZombie24
I was wondering, is there an easy way to reconstruct a class? i am currently doing delete p; p = new theClass(newConstructor);
i rather do something like reconstruct (p, newConstructor);
my_object = theClass(...);

Share this post


Link to post
Share on other sites
A slightly more detailed answer...

If your objects are stored by value, then I'd recommend the above method.

There may be cases though where it's necessary or desirable to store and reference your objects via pointer. In this case I would recommend the use of smart pointers, which (among other things) will streamline the re-creation process a bit:
boost::shared_ptr<theClass> p(new theClass(...));
// ...
p.reset(new theClass(...));
This is similar to your example, except that the cleaning up of the resource is handled automatically by the smart pointer object.

If the newly created object is to be of the same type, another option would be:
*p = theClass(...);
Although unlike with the 'reset()' example, this assumes that p is not empty.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!