views:

1634

answers:

9

Can I write a copy constructor by just passing in a pointer instead of the const reference? (Would it be ok if I make sure that I am not going to change any values though?)

Like so:

SampleClass::SampleClass(SampleClass* p)
{
 //do  the necessary copy functionality
}

instead of:

SampleClass::SampleClass(const SampleClass& copyObj)
{
//do the necessary copy
}

Thanks in advance.

+1  A: 

No. Copy constructors must take a reference, not pointer, if it's to be useful for passing-by-value, etc.

Chris Jester-Young
+4  A: 

You can write a constructor that takes a pointer as an argument.
But the copy constructor is the name we give a specific constructor.
A constructor that takes a reference (preferably const but not required) of the same class as an argument is just named the copy constructor because this is what it effectively does.

Martin York
+16  A: 

Yes, you can write a constructor that takes a pointer to the object. However, it cannot be called a copy constructor. The very definition of a copy constructor requires you to pass an object of the same class. If you are passing anything else, yes, it's a constructor alright, but not a copy constructor.

Frederick
+1  A: 

By definition, the copy ctor uses a const reference. While there is nothing stopping you from writing a ctor that takes a pointer, it raises some problems not present when using a reference - e.g., what should/can happen if a null pointer is passed in?

ceretullis
+1  A: 

A copy constructor needs a reference because a value parameter would require making a copy, which would invoke the copy constructor, which would make a copy of its parameter, which would invoke the copy constructor, which ...

Bill the Lizard
+1  A: 

You can write a constructor like that but its not technically a copy constructor. For example the STL containers will still use the compiler generated copy constructor (the compiler generates one because you didn't write one).

Brian Ensink
A: 

Thanks everyone. So, if I write a constructor that takes in a pointer( and thought that's my copy constructor), the compiler would still supply with the default copy constructor in which case my constructor( which i thought was my copy constructor) would not be called and the default copy constructor would be called. Got it.

vj01
Which constructor is called has nothing to do with which constructors are available. One would never be called due to the absence of the other. Each is called based only on what type of argument you provide.
Rob Kennedy
vj, what exactly are you doing or trying to do that you need to take an argument like that?
GMan
What you say is correct. The default copy constructor will indeed be generated and it, not the one you wrote, be called when needed.
Frederick
A: 

Besides the fact that it would not be a copy constructor and the compiler will generate the copy constructor unless you explicitly disable it, there is nothing to gain and much to loose. What is the correct semantics for a constructor out of a null pointer? What does this add to the user of your class? (Hint: nothing, if she wants to construct out of a heap object she can just dereference the pointer and use the regular copy constructor).

David Rodríguez - dribeas
A: 

The copy constructor is implicitly used in two cases:

  • When an instance of your class is passed by value to a function.
  • When an instance of your class is returned by value from a function.

As others have mentioned, you can write a constructor with the signature described (or with a const pointer), but it would not be used in either of the above cases.

JohnMcG