1
const ClassA& curShot = vec_shots[curShotIndx];

In the above code the curShot object is constructed and assigned at the same step. My question is which constructor is used in the above statement. I thought it will be the default constructor followed by the assignment operator, but it seems to call the copy constructor instead. Why is that the case?

vkaul11
  • 4,098
  • 12
  • 47
  • 79

4 Answers4

4

What happens is that

vec_shots[curShotIndx];

returns a reference which you assign to const ClassA& curShot. There is no object creation involved in this step. Therefore no constructor is invoked (neither default nor copy constructor).

The assignment operator is not invoked either since you are not assigning one object instance to another, but only a reference. You are not handling more than one (existing) object instance in this code. So, no construction or assignment is invoked.

Andreas Fester
  • 36,091
  • 7
  • 95
  • 123
  • Thanks Andreas, just that I had a type above with the &. However, I appreciate your answer because I was not sure what it would do with the & either. – vkaul11 Dec 06 '12 at 11:18
1

No constructor is used, curShot is a reference, an alias to an already existing object, not a stand-alone object by itself.

Also, initialization and assignment cannot be done at the same step. For example, say you had

 ClassA original;
 ClassA copy = original;

Here, copy is not assigned original, it's initalized using original. This is called copy initialization.

If you did

 ClassA copy2(original);

this would be called direct initialization.

The copy constructor would be called in both instances. (copy elision can occur, so it might not be called, but it must be available)

Assignment is when you use operator = on an already existing object:

 ClassA x;
 ClassA y;
 x = y;     //assignment
Community
  • 1
  • 1
Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • -1 *The copy constructor would be called in both instances.* Misleading rather wrong. – Alok Save Dec 06 '12 at 06:38
  • @Als in which one is it not called? – Luchian Grigore Dec 06 '12 at 06:39
  • Whether or not the copy constructor will be called during Copy Initilization completely depends on the class definition.If the class provides an appropriate conversion operator which is a better match than the copy constructor then it will be used. – Alok Save Dec 06 '12 at 06:43
  • @Als I don't follow. Can you provide an example? – Luchian Grigore Dec 06 '12 at 06:44
  • Well the copy constructor call can be simply elided. It implies the copy constructor should be present but not necessary that it will be called. For an example, search copy elision and hopefully you should find one. – Alok Save Dec 06 '12 at 06:55
  • @Als You said something about a conversion operator being a better match. [Copy elision](http://stackoverflow.com/questions/12953127/what-are-copy-elision-and-return-value-optimization) is a whole different story. – Luchian Grigore Dec 06 '12 at 06:57
  • @Als can you please clarify what you mean by "an appropriate conversion operator which is a better match than the copy constructor"? – Luchian Grigore Dec 06 '12 at 07:18
  • No I do not intend to. I do not have sufficient time.Given that I will remove the downvote. Still *The copy constructor would be called in both instances* is wrong. – Alok Save Dec 06 '12 at 07:19
  • @Als were you referring to a case where there are 2 different classes (note that my first 2 samples, the ones I was referring to, the two objects are of the same type). – Luchian Grigore Dec 06 '12 at 07:32
1

Since you wrote "it seems to call copy constructor", I assume the ampersand in your question is a typo.
In that case, if you would do

const ClassA curShot = vec_shots[curShotIndx];

it is evaluated as copy construction. It is just the same as the ugly const ClassA curShot( vec_shots[curShotIndx] ).

However, if you write

ClassA curShot;  // I skipped the "const", because otherwise the example would be invalid.
curShot = vec_shots[curShotIndx]; 

then a default constructor gets called and an opearator= is called on the second line.


Moreover, "=" so much can mean calling NEITHER copy constructor NOR operator=, that you can have this:

const ClassA f(){ return ClassA(); }
//...
const ClassA curShot = f();  // we would expect here a copy constructor call

Here -- if the compiler uses return value optimization and usually it does -- only a default constructor gets called for curShot.

Barney Szabolcs
  • 11,846
  • 12
  • 66
  • 91
0

This statement just define curShot as a reference, it's not a new object.

TieDad
  • 9,143
  • 5
  • 32
  • 58