Because you are creating a new object. operator= applies when
assigning to an object that already exists. Pay no attention to the '='
character allowed largely for compatibility with C syntax. You may find
it more clear to use the first form only.
Actually, they are slightly different.
class A {
public:
A(int);
private:
A(const A&);
};
A ok(1);
A error = 2;
The last line should not compile. It instructs the compiler to create a
temporary A-object from the integer 2 and use the copy constructor to
construct 'error' from this temporary. The compiler is allowed to optimise
this away, but it is not allowed to waive the access priviliges needed.
And as the copyconstructor is private, we cannot construct 'error' from
the temporary A object.
The line that constructs 'ok' is fine.
The lesson is to not use 'T var=value' but 'T var(value)' instead. It
shields you from subteleties like this.
[ For those wondering, the compiler is allowed to optimise the second form
into the first *even* if copy constructing has side effects, so this
class A {
public:
A(int) { puts("A::A(int)"); }
private:
A(const A&) { puts("A::A(const A&)"); }
};
A ok = 1;
Is allowed to print either:
A::A(const A&)
A::A(int)
or only
A::A(int)
]
HTH,
M4