Copy Constructor

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34

#include <iostream>

using namespace std;

class A
{
public:
 A()
 {
  cout << "default A" << endl;
 }
 
 A(const A&) //  copy constructor
 {
  cout << "copy A" << endl;
 }
 
 A f1()
 {
  A a;
  return a;
 }

};

int main()
{
 A a;
 A b =  a.f1(); //  shouldn't here be first of all the default and then copy 
                //  constructor called, please explain why it's not like that

return 0;
}


Thanks
Last edited on
See section 12.6.1 of the C++11 standard.

What you have is a case of explicit intialization. The default constructor is not called in this case.
Isn't this a copy elision ?
Yes, it is.
Topic archived. No new replies allowed.