Jump to content
  • Advertisement
Sign in to follow this  
Aroidzap

C++ how to avoid calling destructor

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

Hi, I've got code like this :

 

 

std::vector<some_class> array;

void pushback_function()
{
    some_class temp;
    array.push_back(temp);
}

 

Is it somehow possible to avoid calling destructor of temp

Share this post


Link to post
Share on other sites
Advertisement
Not really. Why don't you want the destructor called?

If you are trying to save the copy, you can look into C++11's move semantics.

Share this post


Link to post
Share on other sites

Every object that is created needs to be destroyed.  There is a very tiny exception to the rule and that is if you are basically writing your own memory manager.  But I doubt that is the case here.

 

 

 

What are you trying to accomplish?  Why don't you want your destructor to get called?

Share this post


Link to post
Share on other sites
ok... so... is it possible to push_back pointer to "temp" (create temp using some_class *temp = new some_class;) in array? Edited by Aroidzap

Share this post


Link to post
Share on other sites
Perhaps push the memory contained within an std::tr1::shared_ptr onto the vector but set it's deleter function to a function that does nothing.

array.push(std::tr1::shared_ptr<some_class>(new temp(), std::tr1::bind(std::tr1::placeholders::_1, nullFunc));

...

void nullFunc(some_class* unused)
{
  // do nothing...
}

Share this post


Link to post
Share on other sites
Whoever decreased my rep, I am hoping you did so for the nullFunc stuff rather than the std::tr1::shared_ptr because although that looks messy, it is pretty standard C++.

Share this post


Link to post
Share on other sites
There are many things you could do, like having a container of pointers, a container of smart pointers or a Boost pointer container. But unless we know what you are trying to do and why it is a problem that the destructor is being called, we can't really give you much advice.

Share this post


Link to post
Share on other sites

Whoever decreased my rep, I am hoping you did so for the nullFunc stuff rather than the std::tr1::shared_ptr because although that looks messy, it is pretty standard C++.

It's probably because of the misspelled "its". smile.png

Share this post


Link to post
Share on other sites

ok... so... is it possible to push_back pointer to "temp" (create temp using some_class *temp = new some_class;) in array?

Sure, you can create a std::vector<some_class *>, and then push pointers into it instead. Just remember to delete each one when you are actually done with it.

You can also use std::shared_ptr, or (from C++11 onwards) std::unique_ptr, to avoid having to remember to delete the objects in the end.

But I'm curious: how much work is this destructor doing, that you are so adamant to avoid calling it? In general, a copyable class should do very little work in its destructor (and if your class isn't meant to be copyable, well, then you need to take the pointer approach to start with).

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.

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!