I am confused when the move constructor gets called against the copy constructor. I read the following sources:
The move constructor is not called in C ++ 0x
Move rvalue semantics and links in C ++ 11
msdn
All of these sources are either too complex (I just want a simple example) or just show how to write a move constructor, but not what to call it. I wrote a simple problem to be more specific:
const class noConstruct{}NoConstruct; class a { private: int *Array; public: a(); a(noConstruct); a(const a&); a& operator=(const a&); a(a&&); a& operator=(a&&); ~a(); }; a::a() { Array=new int[5]{1,2,3,4,5}; } a::a(noConstruct Parameter) { Array=nullptr; } a::a(const a& Old): Array(Old.Array) { } a& a::operator=(const a&Old) { delete[] Array; Array=new int[5]; for (int i=0;i!=5;i++) { Array[i]=Old.Array[i]; } return *this; } a::a(a&&Old) { Array=Old.Array; Old.Array=nullptr; } a& a::operator=(a&&Old) { Array=Old.Array; Old.Array=nullptr; return *this; } a::~a() { delete[] Array; } int main() { a A(NoConstruct),B(NoConstruct),C; A=C; B=C; }
currently A, B and C have different pointer values. I would like A to have a new pointer, B to have an old pointer to C, and C to have a null pointer.
A bit off topic, but if one could offer documentation where I could learn more about these new features, I would be grateful and probably would not need to ask many more questions.
c ++ c ++ 11 move-constructor
Lauer Oct 29 '12 at 16:22 2012-10-29 16:22
source share