Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy constructors - c++

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.


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.

like image 988
vj01 Avatar asked Apr 14 '09 03:04

vj01


People also ask

What is a copy constructor C?

A copy constructor is a member function that initializes an object using another object of the same class. In simple terms, a constructor which creates an object by initializing it with an object of the same class, which has been created previously is known as a copy constructor.

What do copy constructors do?

Copy constructors are the member functions of a class that initialize the data members of the class using another object of the same class. It copies the values of the data variables of one object of a class to the data members of another object of the same class.

What is copy constructor example?

Copy Constructor is called in the following scenarios: When we initialize the object with another existing object of the same class type. For example, Student s1 = s2, where Student is the class. When the object of the same class type is passed by value as an argument.

How do you clone a constructor?

Copy constructor can be created by explicitly passing the object as an argument of the same class whose object we want to create. Copy of object can also be created without using a copy constructor and by simply assigning the values of one object to the other object or using the clone() method.


3 Answers

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.

like image 72
Frederick The Fool Avatar answered Oct 05 '22 18:10

Frederick The Fool


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.

like image 43
Martin York Avatar answered Oct 05 '22 17:10

Martin York


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).

like image 32
David Rodríguez - dribeas Avatar answered Oct 05 '22 18:10

David Rodríguez - dribeas