What is move semantics in C++11 and why is it needed?
Hello! I just started learning C++11 and came across the term "move semantics". I kind of understand that it has something to do with performance, but in practice I can't figure it out.\n\nLet's say I have a class like this:\n\n```cpp\nclass MyString {\npublic:\n MyString(const char* str) : data_(new char[strlen(str) + 1]) { strcpy(data_, str); }\n ~MyString() { delete[] data_; }\n MyString(const MyString& other) : data_(new char[strlen(other.data_) + 1]) { strcpy(data_, other.data_); }\n char* data_;\n};\n```\n\nI write a function that returns `MyString`:\n\n```cpp\nMyString createHello() {\n MyString s("Hello, World!");\n return s; // What happens here? Copying?\n}\n```\n\nUsing old C++03, when returning from a function, the copy constructor was called, which allocated
No answers yet.
Log in to leave an answer