user1141043
user1141043

Reputation: 29

Is copy constructor called if assignment operator wasn't defined?

Suppose that the foo class does not have an overloaded assignment operator. What happens when an assignment a = b; is given for two foo objects? options are:

  1. The automatic assignment operator is used
  2. The copy constructor

Upvotes: 2

Views: 1579

Answers (1)

Luchian Grigore
Luchian Grigore

Reputation: 258608

It depends:

A a;
//this is not an assignment, it is equivalent to A b(a);
A b = a; //default copy constructor is called

A c;
//assignment
c = a; //default assignment operator is called

Upvotes: 4

Related Questions