I tried to read about it and also found a couple of similar questions on the internet, but they were either not directly related to my issue or a little confusing. My questions are as follows:
- It appears that move semantics are only about the (memory of) objects that are contained in a class - not for the class itself, or the primitive data types in it. If that is correct, is it of any use at all for creating a Move constructor for a class that contains 4 to 5 different integer, long and float member variables, for example? This is a considerable chunk of memory for let's say some high performance application. If all that memory is still going to be allocated for temporary objects in a function and then again when you assign that to a suitable lvalue, it doesn't solve the purpose completely and passing the object as reference in the argument might still be the way to go. Am I correct in this assumption? Is there a way to make it work so that return by value works for my object and the memory for primitive data members is also not allocated twice? (I know I can create a separate class/struct containing all the primitive types to create one object and then including its pointer in my class as a member, but that seems to be a rather clumsy solution.)
- Can defining a move constructor ever be useful for a single primitive member class?
This is not a case of premature optimization, I'm just trying to understand it as it's a little different from what I was expecting.
Edit: @Paul, Nicol here's a simple example of what I'm trying to do. Can I be sure that RVO will take care of creating the object only once(let's say in both MSVC11 and GCC 4.8)?
class MyObject
{
public: //public members for the purpose of this example
long long mNum1;
int mNum2;
public:
MyObject GetModifiedObject()
{
MyObject retVal;
retVal.mNum1 = SomeGlobalMethod1(mNum1);
retVal.mNum2 = SomeGlobalMethod2(mNum2);
return retVal;
}
};
int main(int, char*)
{
MyObject obj;
obj.mNum1 = -2232344;
obj.mNum2 = 25;
MyObject obj2 = obj.GetModifiedObject();
}
Thanks!