Why don't you want the destructor called?
I quoted this post, to re-affirm that it is the "right" question.
If you are trying to save the copy, you can look into C++11's move semantics.
You could emplace_back(), if your compiler(s) support it (C++11 once again).
I quoted the above two posts to re-affirm that they are the "right" answer. Both require C++11.
If you aren't C++11, you could do something like this instead:
std::vector<some_class> array;
void pushback_function()
{
array.push_back(some_class());
array.back().heavyInitializationStuff();
}
But that violates RAII, as so should be a questionable move to make. If you don't have C++11, your heavy class could implement some kind of "swap" function just for itself, to transfer ownership of data pointers or something.
Instead, I'll ask my own "right" question: Is this an actual need for being optimized after measuring your existing application's performance using profiling to find the bottlenecks, or is this pre-optimization? I wouldn't be surprised if your compiler optimized your existing code as it is.
Why is the copy such a huge deal?
Well, your actual question was, how do you avoid the destructor. So I guess my question should be, why is the destructor such a big deal? What are you doing in it?






