views:

165

answers:

2

is that correct to write a constructor like this?

class A
{
    A::A(const A& a)
    {
    ....
    }
};

if yes, then is it correct to invoke it like this:

A* other;
...
A* instance = new A(*(other));

if not, what do you suggest?

Thanks

A: 

No, you are writing a copy constructor. It will not act as you expect (according to the examples provided).

class A{

   A(arguments){ ...}
}

I would suggest reading a C++ book, differences between a constructor and a copy constructor are well explained.

When is it used ? When an instance is copied. For example, by returning /passing an instance by value

void foo (A a){ 
  //copy constructor will be called to create a
}

A bar(){

  return a; //Copy constructor will be called 
}
Tom
A copy **constructor** is certainly a **constructor**.
James McNellis
+5  A: 

Almost correct. When declaring the constructor in the class, simply write A, not A::A; you would use A::A when giving a definition for the constructor outside of the class declaration. Otherwise, yes.

Also, as James points out, unless you are copying from an object that you are accessing via a pointer, you don't need to do any dereferencing (if it is a value or a reference). One typically does not use pointers unless it is necessary to do so. On that principle, you would have something like:

 A x; // Invoke default constructor
 // ...
 // do some thing that modify x's state
 // ...

 A cpy(x); // Invokes copy constructor
           // cpy now is a copy of x.

Note, though, that the first statement A x invokes the default constructor. C++ will provide a default implementation of that constructor, but it might not be what you want and even if it is what you want, it is better style, IMHO, to give one explicitly to let other programmers know that you've thought about it.

Edit
C++ will automatically provide an implementation of the default constructor, but only if you don't provide any user-defined constructors -- once you provide a constructor of your own, the compiler won't automatically generate the default constructor. Truth be told, I forgot about this as I've been in the habit of giving all constructor definitions myself, even when they aren't strictly necessary. In C++0x, it will be possible to use = default, which will provide the simplicity of using the compiler-generated constructor while at the same time making the intention to use it clear to other developers.

Michael Aaron Safyan
A good place for the A::A(...) is in the cpp file, though you could put it at the end of the class definition too. (you could put it most any place but these two places are best practice.
baash05
@baash05, one typically puts the definition in a .cpp file. If one puts it in the header after the class, then it is necessary to declare it "inline" unless you want to deal with ODR headaches. Of course, one can simply give the body in the class declaration (implicitly inline), which eliminates the whole issue.
Michael Aaron Safyan
There is no default constructor. So how can you invoke it here.
Martin York
Sorry, A::A was typo... I mean, I put the class definition later, and forgot to correct that.However, my main concern was the copy constructor.It is kind of a trade off, if you define them as pointers, all the overloaded operators are hard to use... like *A+*Aotherwise, you don't have a control on it's creation. I mean you can not create them later or ...
Nima
@Martin, the compiler provides a default constructor if you don't specify one.
Michael Aaron Safyan
@Michael Aaron Safyan: The compiler generates a default constructor if (and only if) You do not provide any constructors. In this case; there is a user provided copy constructor, therefore there will be no compiler generated default constructor.
Martin York