How to prevent object copy in C++
In this tutorial, we will learn how to prevent object copy in C++. This can be done in 3 ways and we shall implement each of these.
Making the access specifier of Copy constructor and Copy assignment operator private
#include <iostream>
using namespace std;
class Student
{
int roll;
public:
Student() { }
Student(int x)
{
roll=x;
}
private:
// Copy constructor
Student(const Student& obj)
{
roll=obj.roll;
}
// Copy assignment operator
Student& operator=(const Student& obj)
{
roll = obj.roll;
return *this;
}
};
int main()
{
Student s1(10);
Student s2(s1); // calling copy constructor
s2 = s1; // calling copy assignment operator
return 0;
}
Here, when we are calling the copy constructor and copy assignment operator we encounter an error. This is because these two are private. Thus preventing object copying.
Output:
15 3 [Error] 'Student::Student(const Student&)' is private 31 15 [Error] within this context 20 12 [Error] 'Student& Student::operator=(const Student&)' is private 32 5 [Error] within this context
Deleting the Copy constructor and Copy assignment operator
#include <iostream>
using namespace std;
class Student
{
int roll;
public:
Student() { }
Student(int x)
{
roll=x;
}
// Copy constructor
Student(const Student& obj) = delete;
// Copy assignment operator
Student& operator=(const Student& obj) = delete;
};
int main()
{
Student s1(10);
Student s2(s1); // calling copy constructor
s2 = s1; // calling copy assignment operator
return 0;
}
We have deleted both the copy constructor and the copy assignment operator so, while calling them we get errors. Thus preventing object copying.
Output:
23 15 [Error] use of deleted function 'Student::Student(const Student&)' 14 3 [Note] declared here 24 5 [Error] use of deleted function 'Student& Student::operator=(const Student&)' 16 12 [Note] declared here
Through inheritance i.e., by making the Copy constructor and Copy assignment operator private in the base class
#include <iostream>
using namespace std;
class Base
{
public:
Base() { }
private:
// Copy constructor
Base(const Base& obj) { }
// Copy assignment operator
Base& operator=(const Base& obj) { }
};
class Student : public Base
{
int roll;
public:
Student() { }
Student(int x)
{
roll=x;
}
};
int main()
{
Student s1(10);
Student s2(s1); // calling copy constructor
s2 = s1; // calling copy assignment operator
return 0;
}
Both the copy constructor and the copy assignment operator are private in the Base class thus preventing object copying and producing error while doing so.
Output:
In copy constructor 'Student::Student(const Student&)': 10 6 [Error] 'Base::Base(const Base&)' is private 15 7 [Error] within this context In function 'int main()': 30 15 [Note] synthesized method 'Student::Student(const Student&)' first required here In member function 'Student& Student::operator=(const Student&)': 12 12 [Error] 'Base& Base::operator=(const Base&)' is private 15 7 [Error] within this context In function 'int main()': 31 5 [Note] synthesized method 'Student& Student::operator=(const Student&)' first required here
This is how we prevent object copy in C++.
You may also read,
Copy Constructor in C++
Inheritance in C++
Leave a Reply