views:

141

answers:

4

I tried to use copy constructor using statement:

X y = X();

But copy constructor is not being called. I am using g++ 4.1.0. I set both X(const X&) and X(x&) constructor in the class.

Is this supposed to work or I am doing some very basic problem in the code?

My code for the class is

class A
{
 public:
  int i;

  A(int ii)
  {
   i = ii;
  }

  A(const A&)
  {
   i = 5;
  }

  A(A&)
  {
   i = -1;
  }

  A()
  {
   i = 5000;
  }
};

When I use it using A a = A(); or A a = A(100);, it does not work but when i use it A a(b); or A a = b; it works fine.

What is the point I am missing? I saw that according to wikipedia , it should work but it's not working in my case :(.

Thanks in advance for all your answers and comments.

+1  A: 
X y = X();

calls the default constructor. The copy constructor is the one that takes a reference to an instance you want copied.

The point of a copy constructor is to take another object of the same type, and make a copy of it. Everything else is not a copy constructor.

romkyns
ie declared as X(X
Mark
Actually X() will default construct a temporary object, but y= should call the copy constructor, since it's declared and assigned at the same time.
Marcin
Having seen the top answer I realise that my answer completely misses the point. Should I delete it? :)
romkyns
The comment I made to @Dunya applies here as well.
Jerry Coffin
+13  A: 

The compiler is permitted to elide the call to the copy constructor in certain situations. Initializing an object from a temporary is one of them. In this case, the temporary is simply constructed in-place instead of constructing a temporary and then copying it into the named object.

You can call the copy constructor by constructing a named object then making a copy of that:

X x;
X y = x;
James McNellis
I thought the compiler is __always__ allowed to elide copying?
sbi
@sbi: There are only two times that copy elision may take place: (1) when a named object is initialized with a temporary object, and (2) when an object is returned from a function (these two are listed in §12.8/15).
James McNellis
@James Thanks a lot for your explanation. Now I understand the exact reason for the behavior. I think I should read C++ standards before shooting questions on stackoverflow but reading standards is not fun but asking questions is :).
Atul
@Atul: I wouldn't read the standard. It's long, boring, and extraordinarily difficult to read and comprehend. A good C++ book would cover this sort of thing, though.
James McNellis
@James Thanks. I thought you have been reading standards looking at the you knowledge :) but I also agree a good C++ should cover these sort of things.
Atul
A: 

The copy constructor is called by the statements X x(y); or X x = y;.

When you call X x = X();, the default constructor is called.

When you call X x = X(100);, a constructor with one parameter is called. These are not copy constructors.

Dunya Degirmenci
Officially, `X x = X();` default constructs a temporary, then copy constructs `x` from the temporary (but the copy can elided even if the copy ctor has side effects -- and most compilers do this, so you won't normally see it).
Jerry Coffin
A: 

Copy constructors are called when you initialize an object with an another object:). In your first example it is totally natural that the copy ctors are not called, only constructors with the suitable parameter lists will be called.

LostMohican
Ideally, Copy Constructor should be called whenever a new object is being created(copied) from existing object and suitable copy constructor arguments are present in the class file.
Atul