What errors does the following code contain?

class A {
    public:
        A(int x) : _x(x) {} //1
    private:
        int _x;
};

int main() { 
    A a = 10; //2
    A b(a);   
    return 0; 
}
Explanation
There are no mistakes in line //1, there is a regular initialization list.
In line //2 object a will be initialized either directly via existing constructor A(int) (if copy elision enabled), or from the temporary object generated by the A(int) constructor and then will be copied into a via automatically generated copy constructor. The copy constructor is automatically generated by the compiler if there are no explicitly declared copy constructor.
Object b will be initialized by the automatically generated copy constructor.

@hrendashtam23 you can avoid using a semicolon. You can also check an explanation below the question - it has link on wiki with similar example

2017 Dec 1, 6:22:07 PM

Is a semicolon needed at the end of the line 1?

2017 Nov 19, 7:00:16 AM

Следи за CodeGalaxy

Мобильное приложение Beta

Get it on Google Play
Обратная Связь
Cosmo
Зарегистрируйся сейчас
или Подпишись на будущие тесты