I have a bit of code that I'm having some trouble with. It looks something like this (I broke it down to make it short and sweet):
Code:
#include <vector>
#include <string>
class MyClass
{
public:
std::string some_string;
double some_double;
MyClass() : some_string(""), some_double(0.0){}
};
void function(std::vector <MyClass>*, std::vector <MyClass>*);
int main(void)
{
std::vector <MyClass>* first_object = new std::vector <MyClass>;
std::vector <MyClass>* second_object = new std::vector <MyClass>;
function(first_object, second_object);
delete first_object;
first_object = NULL;
delete second_object;
second_object = NULL;
return 0;
}
void function(std::vector <MyClass>* first_object, std::vector <MyClass>* second_object)
{
//Do stuff to first_object.
//...
//Do stuff to second_object.
//...
*first_object = *second_object;
}
The problem is, *first_object and *second_object can both be very large, and function() can be called many times. The assignment at the end of function() is creating an enormous bottleneck in my program. After the assignment is done, I'm only concerned about the contents of *first_object; second_object isn't used outside of function() and it's contents are re-written in function() each call. So I figure an easy way to speed up execution would be to swap the pointers rather than do the costly assignment. So, at the end of function(), I want to do something like this:
Code:
std::vector <MyClass>* temp_ptr = first_object;
first_object = second_object;
second_object = temp_ptr;
However, that code isn't doing what I want it to, and for the life of me, I can't figure out why. Any words of advice?