Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between these two prototypes?

Tags:

c++

 1.  A a;
     A b = a;
 2.  A a,b;
     b = a;

What is the difference between these two operator =?

Does the first one needs a prototype?


2 Answers

In 1, a is default-constructed and b is copy-constructed from a. There is no assignment going on.

In 2, both a and b are default-constructed and then the value of a is assigned to b.

Both of these are not prototypes, instead it is creating objects of class A. In the first case statement A b = a; invokes the copy constructor of A where as the second case A a,b uses the default constructor of A to create objects and then use assignment operator of A for b=a.

like image 35
Naveen Avatar answered Jun 30 '26 18:06

Naveen