What will be displayed:
#include <iostream>

class A
{
    public:
    A() { std::cout << "A()"; }

    A(const A& a){ std::cout << "copyA()"; }

    A& operator=(const A& a)
    {
        std::cout << "=()";
    }
};

int main()
{
    A a1;
    A a2 = a1;
}
Explanation
Program displays:
A()copyA()
Copy-constructor is called when a new object is created from an existing object, as a copy of the existing object. And assignment operator is called when an already initialized object is assigned a new value from another existing object.

a2 = a1;  // calls assignment operator, same as "a2.operator=(a1);"
A a3 = a1; // calls copy constructor, same as "A a3(a1);"

Слідкуй за CodeGalaxy

Мобільний додаток Beta

Get it on Google Play
Зворотній Зв’язок
Cosmo
Зареєструйся Зараз
або Підпишись на майбутні тести