• C++ Data Types
  • C++ Input/Output
  • C++ Pointers
  • C++ Interview Questions
  • C++ Programs
  • C++ Cheatsheet
  • C++ Projects
  • C++ Exception Handling
  • C++ Memory Management
  • AI Prolog Installation in Turbo C++
  • Signal Handling in C++
  • Cross-platform Windows / Raspberry Pi project with C++, OpenCV and Gtk
  • Strict Type Checking in C++
  • is_void template in C++
  • Introduction to Complex Objects and Composition
  • How to quickly swap two arrays of same size in C++?
  • OpenCV C++ Windows Setup using Visual Studio 2019
  • random header in C++ | Set 3 (Distributions)
  • Is assignment operator inherited?
  • C++ Installation on MacBook M1 for VS Code
  • wcslen() function in C++ with Examples
  • Fork CPP | Course Structure
  • RAPTOR Tool - A Flowchart Interpreter
  • Nested Classes in C++
  • Representation of Int Variable in Memory in C++
  • Local Classes in C++
  • Application of OOPs in C++

Copy Constructor vs Assignment Operator in C++

Copy constructor and Assignment operator are similar as they are both used to initialize one object using another object. But, there are some basic differences between them:

Consider the following C++ program. 

Explanation: Here, t2 = t1;  calls the assignment operator , same as t2.operator=(t1); and   Test t3 = t1;  calls the copy constructor , same as Test t3(t1);

Must Read: When is a Copy Constructor Called in C++?

Please Login to comment...

  • How to Delete Whatsapp Business Account?
  • Discord vs Zoom: Select The Efficienct One for Virtual Meetings?
  • Otter AI vs Dragon Speech Recognition: Which is the best AI Transcription Tool?
  • Google Messages To Let You Send Multiple Photos
  • 30 OOPs Interview Questions and Answers (2024)

Improve your Coding Skills with Practice

 alt=

What kind of Experience do you want to share?

This browser is no longer supported.

Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.

Copy constructors and copy assignment operators (C++)

  • 8 contributors

Starting in C++11, two kinds of assignment are supported in the language: copy assignment and move assignment . In this article "assignment" means copy assignment unless explicitly stated otherwise. For information about move assignment, see Move Constructors and Move Assignment Operators (C++) .

Both the assignment operation and the initialization operation cause objects to be copied.

Assignment : When one object's value is assigned to another object, the first object is copied to the second object. So, this code copies the value of b into a :

Initialization : Initialization occurs when you declare a new object, when you pass function arguments by value, or when you return by value from a function.

You can define the semantics of "copy" for objects of class type. For example, consider this code:

The preceding code could mean "copy the contents of FILE1.DAT to FILE2.DAT" or it could mean "ignore FILE2.DAT and make b a second handle to FILE1.DAT." You must attach appropriate copying semantics to each class, as follows:

Use an assignment operator operator= that returns a reference to the class type and takes one parameter that's passed by const reference—for example ClassName& operator=(const ClassName& x); .

Use the copy constructor.

If you don't declare a copy constructor, the compiler generates a member-wise copy constructor for you. Similarly, if you don't declare a copy assignment operator, the compiler generates a member-wise copy assignment operator for you. Declaring a copy constructor doesn't suppress the compiler-generated copy assignment operator, and vice-versa. If you implement either one, we recommend that you implement the other one, too. When you implement both, the meaning of the code is clear.

The copy constructor takes an argument of type ClassName& , where ClassName is the name of the class. For example:

Make the type of the copy constructor's argument const ClassName& whenever possible. This prevents the copy constructor from accidentally changing the copied object. It also lets you copy from const objects.

Compiler generated copy constructors

Compiler-generated copy constructors, like user-defined copy constructors, have a single argument of type "reference to class-name ." An exception is when all base classes and member classes have copy constructors declared as taking a single argument of type const class-name & . In such a case, the compiler-generated copy constructor's argument is also const .

When the argument type to the copy constructor isn't const , initialization by copying a const object generates an error. The reverse isn't true: If the argument is const , you can initialize by copying an object that's not const .

Compiler-generated assignment operators follow the same pattern for const . They take a single argument of type ClassName& unless the assignment operators in all base and member classes take arguments of type const ClassName& . In this case, the generated assignment operator for the class takes a const argument.

When virtual base classes are initialized by copy constructors, whether compiler-generated or user-defined, they're initialized only once: at the point when they are constructed.

The implications are similar to the copy constructor. When the argument type isn't const , assignment from a const object generates an error. The reverse isn't true: If a const value is assigned to a value that's not const , the assignment succeeds.

For more information about overloaded assignment operators, see Assignment .

Was this page helpful?

Coming soon: Throughout 2024 we will be phasing out GitHub Issues as the feedback mechanism for content and replacing it with a new feedback system. For more information see: https://aka.ms/ContentUserFeedback .

Submit and view feedback for

Additional resources

IMAGES

  1. Copy Constructor in C++

    c call copy constructor in assignment operator

  2. Difference between copy constructor and assignment operator in c++

    c call copy constructor in assignment operator

  3. Copy Constructor vs Assignment Operator,Difference between Copy

    c call copy constructor in assignment operator

  4. Copy Constructor in C++ ~ TUTORIALTPOINT- Java Tutorial, C Tutorial

    c call copy constructor in assignment operator

  5. Difference Between Copy Constructor And Assignment Operator In C++ #183

    c call copy constructor in assignment operator

  6. Copy Constructor Array in C++

    c call copy constructor in assignment operator

VIDEO

  1. Assignment Operator in C Programming

  2. Java programming Math operator

  3. #07 Comparable and Comparator In Java 8, Lambda Expression In Hindi || Lambda Expression in Java

  4. Augmented assignment operators in C

  5. C++ Constructors: Types and Copy Constructors || Constructors in OOP || #codewithredoy

  6. Assignment Operator in C Programming

COMMENTS

  1. Copy Constructor vs Assignment Operator in C++

    But, there are some basic differences between them: Copy constructor. Assignment operator. It is called when a new object is created from an existing object, as a copy of the existing object. This operator is called when an already initialized object is assigned a new value from another existing object. It creates a separate memory block for ...

  2. Copy Constructors and Assignment Operators

    Copy Constructors. Whenever you initialize an object, C++ will make the copy by invoking the class's copy constructor, a constructor that accepts another object of the same type as a parameter. Copy constructors are invoked whenever: 1. A newly-created object is initialized to the value of an existing object. 2.

  3. Copy constructors and copy assignment operators (C++

    Use an assignment operator operator= that returns a reference to the class type and takes one parameter that's passed by const reference—for example ClassName& operator=(const ClassName& x);. Use the copy constructor. If you don't declare a copy constructor, the compiler generates a member-wise copy constructor for you.