×

Preventing Object Copy in C++

C++ is an object-oriented programming language that provides the ability to create objects, define class and pass objects to functions. When passing an object to a function or returning an object from a function, a copy of the object is usually created. However, in some cases, it is necessary to prevent object copying in C++. In this article, we will discuss why we may want to prevent object copying and how to do it in C++.

Why Prevent Object Copying?

There are several reasons why we may want to prevent object copying in C++. One of the most common reasons is to prevent unintended side-effects when copying objects. For example, consider an object that represents a unique resource, such as a file handle. When you copy this object, you are creating two references to the same resource. This can lead to unintended behavior when one of the objects is used to modify the resource, as the changes will affect both objects.

Another reason to prevent object copying is to improve performance. Copying large objects can be a time-consuming operation, especially if the objects contain many data members. Preventing object copying can help to reduce the amount of time spent copying objects and can help to improve overall performance.

How to Prevent Object Copying in C++:

There are several ways to prevent object copying in C++.

1. Private Copy Constructor and Assignment Operator:

One of the most common methods of preventing object copying is to declare the copy constructor and assignment operator as private members in the class definition. This makes them inaccessible to client code and prevents the creation of objects using these methods.

Here's an example of a class definition that prevents object copying using this method:

class NonCopyable
{
private:
    NonCopyable(const NonCopyable&) = delete;
    NonCopyable& operator=(const NonCopyable&) = delete;


public:
    NonCopyable() = default;
    virtual ~NonCopyable() = default;
};

In the example above, the copy constructor and assignment operator are declared as private members and are deleted using the delete keyword. This ensures that objects of this class can only be created and assigned using the default constructor and default assignment operator.

Program:

#include <iostream>




class NonCopyable
{
private:
    NonCopyable(const NonCopyable&) = delete;
    NonCopyable& operator=(const NonCopyable&) = delete;


public:
    NonCopyable() = default;
    virtual ~NonCopyable() = default;
};


int main()
{
    NonCopyable obj1;
    // NonCopyable obj2 = obj1; // error: copy constructor is deleted
    // NonCopyable obj3;
    // obj3 = obj1; // error: assignment operator is deleted


    std::cout << "Objects created successfully!" << std::endl;
    return 0;
}

Explanation:

  • The code defines a class NonCopyable that is designed to prevent the creation of objects using the copy constructor and assignment operator.
  • The class definition has two private member functions, the copy constructor and the assignment operator, which are declared using the delete keyword.
  • This keyword indicates that the copy constructor and the assignment operator are inaccessible to client code and will result in a compile-time error if used.
  • The class also has a default constructor and a virtual destructor, which are both declared using the default keyword.
  • In the main function, objects of the NonCopyable class are created using the default constructor. Attempts to copy the objects using the copy constructor or the assignment operator result in compile-time errors, as these functions have been deleted.
  • Finally, a message is displayed indicating that the objects have been created successfully.

Program Output:

Preventing Object Copy in C++

2. Make the copy constructor and the copy assignment operator private members of the class:

Another way to prevent object copying in C++ is to make the copy constructor and the copy assignment operator private members of the class and not provide a definition for them. This makes them inaccessible to client code, and if they are used, it results in a linker error.

For example:

class NonCopyable
{
private:
    NonCopyable(const NonCopyable&);
    NonCopyable& operator=(const NonCopyable&);


public:
    NonCopyable() = default;
    virtual ~NonCopyable() = default;
};

In this example, the copy constructor and the copy assignment operator are declared as private member functions but not defined. This makes them inaccessible to client code and results in a linker error if they are used.

This method is similar to the previous method, but it allows objects of the class to be passed by value or returned by value from functions, since the copy constructor and copy assignment operator are implicitly defined by the compiler. However, it also means that it is not immediately clear to client code that the objects of the class are not meant to be copied.

Program:

#include <iostream>


class NonCopyable
{
private:
    NonCopyable(const NonCopyable&);
    NonCopyable& operator=(const NonCopyable&);


public:
    NonCopyable() = default;
    virtual ~NonCopyable() = default;
};


int main()
{
    NonCopyable obj1;
    // NonCopyable obj2(obj1); // error: copy constructor is private
    // NonCopyable obj3;
    // obj3 = obj1; // error: assignment operator is private


    std::cout << "Objects created successfully!" << std::endl;
    return 0;
}

Explanation:

  • In this program, a class NonCopyable is defined that has a private copy constructor and copy assignment operator. This means that these member functions can only be used by other members and friend functions of the NonCopyable class and are inaccessible to client code.
  • In the main function, an object obj1 of the NonCopyable class is created.
  • Attempts to create another object obj2 by copying obj1 using the copy constructor and to assign obj1 to another object obj3 using the copy assignment operator are commented out, as they would result in compile-time errors, due to the private access specifier for the copy constructor and copy assignment operator.
  • The program outputs the message "Objects created successfully!", indicating that the objects were created without any issues.

Program Output:

Preventing Object Copy in C++

3. final Keyword:

Another method to prevent object copying is to declare the class as final. The final keyword in C++11 prevents the class from being inherited, which in turn prevents the creation of objects using the copy constructor or assignment operator.

Here's an example of a class definition that prevents object copying using the final keyword:

class NonCopyable final
{


public:
    NonCopyable() = default;
    virtual ~NonCopyable() = default;
};

In the example above, the final keyword is used to declare the class as non-inheritable, preventing the creation of objects using the copy constructor or assignment operator.

Program:

#include <iostream>


class NonCopyable final
{


public:
    NonCopyable() = default;
    virtual ~NonCopyable() = default;
};


int main()
{
    NonCopyable obj1;
    // NonCopyable obj2 = obj1; // error: copy constructor is deleted
    // NonCopyable obj3;
    // obj3 = obj1; // error: assignment operator is deleted


    std::cout << "Objects created successfully!" << std::endl;
    return 0;
}

Explanation:

This program demonstrates another way of preventing object copying in C++, using the final keyword. The code defines a class NonCopyable that is designed to prevent the creation of objects using the copy constructor and assignment operator.

The class definition uses the final keyword to indicate that the class cannot be subclassed. This has the side effect of making the copy constructor and assignment operator inaccessible to client code, thus preventing object copying.

The class has a default constructor and a virtual destructor, both declared using the default keyword.

In the main function, objects of the NonCopyable class are created using the default constructor. Attempts to copy the objects using the copy constructor or the assignment operator result in compile-time errors, as these functions are inaccessible due to the use of the final keyword.

Finally, a message is displayed indicating that the objects have been created successfully.

Program Output:

Preventing Object Copy in C++

4. Inherit a Dummy class with a private copy constructor and a private copy assignment operator:

One way to prevent object copying in C++ is to inherit a dummy class that has a private copy constructor and a private copy assignment operator. This dummy class can be used as a base class for the class that needs to prevent object copying.

Here's an example of how this technique can be implemented:

Program:

#include <iostream>


// Define a dummy class with private copy constructor and copy assignment operator.
class Dummy
{
private:
    Dummy(const Dummy&);
    Dummy& operator=(const Dummy&);
};


// Define a class that needs to prevent object copying.
class NonCopyable : public Dummy
{
public:
    NonCopyable() = default;
    virtual ~NonCopyable() = default;
};


int main()
{
    NonCopyable obj1;
    // NonCopyable obj2(obj1); // error: copy constructor is private
    // NonCopyable obj3;
    // obj3 = obj1; // error: assignment operator is private


    std::cout << "Objects created successfully!" << std::endl;
    return 0;
}

Explanation:

  • This program demonstrates how to prevent object copying in C++ using inheritance.
  • The program defines a dummy class named Dummy which has a private copy constructor and a private copy assignment operator.
  • These private functions make it impossible to create a new instance of the Dummy class by copying an existing object.
  • Next, the program defines another class named NonCopyable which inherits from the Dummy class.
  • By inheriting from Dummy, NonCopyable also acquires the private copy constructor and copy assignment operator, making it impossible to copy instances of the NonCopyable class.
  • In the main function, an instance of NonCopyable named obj1 is created.
  •  If we try to create another instance of NonCopyable by copying obj1, the program will give an error because the copy constructor is private.
  • Similarly, if we try to assign obj1 to another instance of NonCopyable, the program will give an error because the copy assignment operator is private.
  • Despite these errors, the program still runs and outputs "Objects created successfully!" to the console.

Error:

Preventing Object Copy in C++

Related Topics

Handling multiple clients on the Server with multithreading using Socket Programming in C or C++

To understand this guide completely, the reader is assumed to be familiar with the foundations of server and client models and socket programming. If one wants to create any scalable...

7 minutes read.

C++ Program to find the product array puzzle

Write a C++ program to form a product array from arr[] where product[i] is the product of all the array elements except arr[i]. Example Input: arr[]  = {10, 3, 5, 6,...

6 minutes read.

C++ Recursion Function

A programming method called recursion that uses a function to call itself to address lesser problems. The Fibonacci sequence, factorial computation, and tree traversal are just a few of the...

4 minutes read.

strcat() vs strncat() in C++

In this tutorial, we will explore about strcat() and strncat() in the most usable language C++. We will also look at the difference between them. strcat() C++ is a computer language with...

4 minutes read.

Inheritance and Friendship in C++

In this tutorial, we will look into what Inheritance and Friendship in C++ are, as well as the differences between the two. What is Inheritance in C++: In C++, inheritance is an...

2 minutes read.

Boost split in C++ library

Boost::split in C++ library Boost offers strong tools for adding mature, well-tested libraries to the C++ standard library. The boost: split function, which is a component of the Boost string algorithm...

2 minutes read.

Hybrid Inheritance

C++ Hybrid Inheritance When more than one type of inheritance is combined in single inheritance is called as hybrid inheritance. C++ Hybrid Inheritance Example #include<iostream>   using namespace std;   class Student{       protected:           int rollno;       public:           void getRoll(int a){               rollno=a;           }           void putRoll(void){               cout <<"Roll No: "<< rollno<<endl;           }   };   class Test : public Student{       protected:           float subject1, subject2;       public:           void getMarks(float x, float y){               subject1=x;               subject2=y;           }           void putMarks(void){               cout<< "Marks gain: "<<endl <<"Subject1 =  "<<subject1<<endl<<"Subject2 = "<<subject2 <<endl;           }   };   class Sport{       protected:           float score;       public:           void getScore(float s){               score=s;           }           void putScore(void){               cout<<"Sports score: "<<score<<endl;           }   };   class Result : public Test, public Sport{       float total;       public:           void display(void);   };   void Result:: display(void){       total=subject1+subject2+score;       putRoll();       putMarks();       putScore();       cout<<"Total Score: "<<total<<endl;   }   int main(){       Result stu;       stu.getRoll(10);       stu.getMarks(40,50);       stu.getScore(60);       stu.display();       return 0;   } Output: Roll No: 10 Marks gain: Subject1 = 40 Subject2 = 50 Sports score: 60 Total...

1 minute read.

C++ Fibonacci Series

What is a Fibonacci series? A Fibonacci series or sequence is a very popular programming paradigm. The next element occurring in the N terms series is determined by the sum of...

2 minutes read.

Advantage and disadvantage friend function C++

Friend Function: - A friend function in C++ is a function that can access the private, protected, and public members of a class. In C++, a friend function is a function that...

2 minutes read.

Fast Input and Output in C++

In competitive programming, it's critical to read input as quickly as possible in order to save time. "Warning: Big I / O data, be aware of certain languages (but most...

3 minutes read.

C++ Maximum Index Problem

Given an array A[] of positive integers. We will find the maximum of (j-i) such that i and j are the indexes of A[] and A[i] <= A[j], i<=j For...

5 minutes read.

C++ Identifier

In a program, C++ identifiers relate to the names of variables, functions, arrays, and other user-defined data types that the programmer has developed. They are a prerequisite for learning any...

4 minutes read.

wcscpy(), wcslen(), wcscmp() Functions in C++

There are many built-in functions in C++ programming language which differentiate it from C programming language in most hardware-coded languages. We will now closely look into the applications of three...

4 minutes read.

ATM machine program in C++ using functions

Automated Teller Machines (ATMs) carry out daily financial transactions. They are straightforward and simple, allowing customers to complete self-service transactions quickly. ATMs can then be used to withdraw cash, deposit...

3 minutes read.

Smart pointers in C++

What are Pointers ? Pointers are often used to keep track of a variable's address. A null value can be assigned to a pointer. Pass by reference can be used to...

6 minutes read.

Array program in C++

What is an Array? An array is a set of identically typed elements that are organized into contiguous memory locations and each element can be independently accessed using an index. We can...

16 minutes read.

Initialize Array of objects with parameterized constructors in C++

Initialize Array of objects with parameterized constructors in C++ When a class is defined, only the specification for the object is specified; no memory or capacity is allocated. You need to...

3 minutes read.

Constructor Vs Destructor in C++

C++ Constructor A function Object () { [native code] } is a member function that shares the same name as the class. It is called automatically whenever a class object is...

3 minutes read.

Substring in C++

A substring is a function in c++ that is imported from the string library. An element of a string is called a substring. A substring contains two parameters length and...

2 minutes read.

Features of OOPS in C++

What is OOPs? The main reason programmers prefer C ++ language over C is because of the support of object-oriented programing in C++. As the name suggests, object-oriented programming or OOPs...

3 minutes read.