What will be displayed:
#include <iostream>

class A
    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;
Program displays:
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
Обратная Связь
Зарегистрируйся сейчас
или Подпишись на будущие тесты