Site icon Learn C++

Learn Copy Constructors in C++ Classes

Learn Copy Constructors in C++ Classes

In C++, constructors are one of the important parts of class types. There are different constructor types in C++ classes and the Copy Constructor is one of these. Copy Constructors are not only used in classes but also used with struct and union data types. In this post, we will try to explain the types of Copy Constructors in modern C++.

What is the Constructor in C++ classes?

The Constructor in C++ is a function, a method in the class, but it is a ‘special method’ that is automatically called when an object of a class is created. We don’t need to call this function. Whenever a new object of a class is created, the Constructor allows the class to initialize member variables or allocate storage. This is why the name Constructor is given to this special method. Here is a simple constructor class example below.

[crayon-6684579137fc4297135998/]

What is Copy Constructor in C++?

The Copy Constructor in classes (i.e class_name) is a non-template constructor whose first parameter is class_name&‍const class_name&‍volatile class_name&‍, or const volatile class_name&‍ . It can be used with no other parameters or with the rest of the parameters all have default values.

The Copy Constructor is a constructor type for classes that class_name must name the current class, or it should be a qualified class name when it is declared at namespace scope or in a friend declaration.

What are the types of Copy Constructors in C++ ?

Now let’s see different types of copy constructors in usage. First, let’s learn deceleration of a copy constructor.

1. Typical Declaration of a Copy Constructor

Syntax to declare Copy Constructor,

[crayon-6684579137fcf655558386/]

Syntax to use Copy Constructor, this copy source_class to new_class as below,

[crayon-6684579137fd2285207908/]

An example with a class;

[crayon-6684579137fd4269419361/]

and the output will be,

[crayon-6684579137fd5697470718/]

Here are more details and a full example about this feature.

2. Forced Copy Constructor (Default Copy Constructor)

In C++, we can force a copy constructor of a class to be generated by the compiler.

Syntax,

[crayon-6684579137fd7773053650/]

An example with a class,

[crayon-6684579137fdb905454212/]

Now, this is same as example before and copy constructor copies not only class properties and methods but also copies values of current class without and definition by user. This method is very useful to copy all parameters in one time. Result will be as follows,

[crayon-6684579137fdd669419677/]

Here are more details and a full example about this feature,

3. Avoiding Implicit Generation of the Copy Constructor (Deleted Copy Constructor)

Deleted Copy Constructor is used if you are Avoiding implicit generation of the copy constructor.

Syntax,

[crayon-6684579137fdf215279848/]

In example, copy constructor can be defined with attributes as delete;

[crayon-6684579137fe1542309275/]

Here are more details and a full example about this feature,

4. Implicitly-Declared Copy Constructor

In C++, the compiler declares a Copy Constructor as a non-explicit inline public member of its class If a copy constructor is not defined for a class type (struct, class, or union), We can Implicitly declare a copy constructor when defining a new class. Remember that copy constructor has this syntax,

[crayon-6684579137fe3404414126/]

and this Copy Constructor will be declared implicitly when declaring a new class as below,

[crayon-6684579137fe5533984084/]

Here is an example,

[crayon-6684579137fe6527400543/]

Here are more details and a full example about this feature,

5. Implicitly-Defined Copy Constructor

The implicitly-defined copy constructor is defined by the compiler if Implicitly Declared Copy Constructor is not deleted. In union types this implicitly-defined copy constructor copies the object representation by using std::memmove statement. In other non-union class types (like classes and structs), their constructor performs full member-wise copy of the object’s bases and non-static members, in their initialization order, using direct initialization. Remember that copy constructor has this syntax,

[crayon-6684579137fe8308270497/]

and this Copy Constructor will be defined implicitly when declaring a new class as below,

[crayon-6684579137feb247882671/]

The generation of the implicitly-defined copy constructor is deprecated if T has a user-defined destructor or user-defined copy assignment operator.

Here are more details and full example about this feature.

6. Deleted Implicitly-Declared Copy Constructor

In C++, we can implicitly declare a copy constructor while it is deleted in previous class. The implicitly-declared or defaulted copy constructor for a class is defined as deleted if,

The compiler declares a Copy Constructor as a non-explicit inline public member of its class If a copy constructor is not defined for a class type (struct, class, or union), We can Implicitly declare a copy constructor when defining a new class.

Remember that copy constructor has this syntax,

[crayon-6684579137fed535987889/]

and this Copy Constructor will be declared implicitly when declaring a new class as below,

[crayon-6684579137fef003500356/]

If we combine both with a Deleted copy Constructor, here is an Deleted Implicitly-Declared Copy Constructor example

[crayon-6684579137ff1772860390/]

Here are more details and a full example about this feature,

7. Trivial Copy Constructor

A Trivial Copy Constructor for a non-union class effectively copies all properties (scalar sub objects, all sub objects of sub objects) of the argument and performs no other action. However, padding bytes need not be copied, and even the object representations of the copied sub objects need not be the same as long as their values are identical. In general, the syntax to declare Copy Constructor is the following:

[crayon-6684579137ff3012862969/]

If you don’t declare anything as above, the class has a Copy Constructor, and a new subclass may have this Copy Constructor which is called a Trivial Copy Constructor. Syntax to use Copy Constructor which copies source_class to new_class as below,

[crayon-6684579137ff5259417757/]

The copy constructor for a class is trivial if all below is maintained,

and also note that,

Here are more details and a full example about this feature,

8. Eligible Copy Constructor

Since C++11, a copy constructor is defined as eligible if it is not deleted.

Since C++20, a copy constructor is eligible if

The triviality of eligible copy constructors determines whether the class is an implicit-lifetime type and whether the class is a trivially copyable type.

[crayon-6684579137ff7927544167/]

Here are more details and a full example about this feature,

C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.

There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise version.

Exit mobile version