vendredi 7 mai 2021

C++ only one constructor is called

I'm trying to understand rvalue reference. This is the code I've written so far:

class A {
public:
    A(const char* str) {
        std::cout << str;
    }

    A(A&& other) {
        std::cout << "other";
    }
};

int main() {
    A m(A(A(A("hello"))));
}

The output is only "hello", which makes me confused.

Since A("hello") is a temporary object passed to the second constructor, But the code output is as if only the first constructor is called.

I guess either this is a compiler optimization or i miss some details about constructors and rvalues.

Aucun commentaire:

Enregistrer un commentaire