Sign in to follow this  

constructor/reconstruct

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

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

This topic is 3593 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this