vendredi 2 janvier 2015

Using Move Constructor

I wrote the class below and I have a question about it. The function sayHello() returns an object which is going to be destroyed after that line. However, it doesn't use move constructor. Isn't it an rvalue reference? Why it doesn't use move constructor?



class FString {
private:
char *ptr;
public:
FString() : ptr(nullptr) {}

FString(const char *str) {
cout << "Called: FString(const char *str)\n";
//...
}

~FString() {
cout << "Called: ~FString()\n";
//...
}

FString(FString &&s) {
cout << "Called: FString(FString &&s)\n";
//...
}
};


FString sayHello() {
FString s("Hello World!");
return s;
}

int main() {
FString s("Hello World!");
FString s2(sayHello());
return 0;
}


Output:



Called: FString(const char *str)
Called: FString(const char *str)
Called: ~FString()
Called: ~FString()


Thanks.


Aucun commentaire:

Enregistrer un commentaire