[Solved] Why parameter can be passed to constructor by assignment operator at object initialization? [closed]


A constructor like the one shown in your question could be called a conversion constructor.

It allows the compiler to take a value of the argument type and convert it to an instance of the class.

In your example, the line

a = b;

is equal to

a = a(5);

(Well, with the exception of the conflict of having both a class and a variable named a)

If you want to disallow it, you have to make the constructor explicit

class A
{
public:
    explicit A(int);
};

Then the compiler can not use the constructor for conversions as the one described above. You have to explicitly use the constructor.

1

solved Why parameter can be passed to constructor by assignment operator at object initialization? [closed]