lundi 23 juillet 2018

Creating an object in c++, difference between these two instantiations? [duplicate]

This question already has an answer here:

I am new to c++ and I come from a c# world, I can't not understand the difference between instantiations:

struct A
{
    A() {
        cout << "A's constructor" << endl;
    }
    A(const A& rhs)
    {
        cout << "A's copy constructor" << endl;
    }

};

int main()
{

    A a = A(); // creation 1
    A a();     // creation 2

    A&& a = A() // creation 3
    getchar();
    return 0;
}

Is there any difference between creation 1 and creation 2. I understand that creation 1 gets created in the stack but so does the creation 2 as well, or are they just equivalent, if so what it the difference between creation 3 and creation 1, I know creation 3 is an R value reference way of doing it. Also why in the creation 1 doesn't "=" operator get overloaded and call the copy constructor. I mean you do first create the object then copy it to a. Any insight is appreciated.

Aucun commentaire:

Enregistrer un commentaire