In the C++ language, one of the features of object-oriented programming (OOP) is the copy assignment operator that is used with “operator=” to create a new object from an existing one. In this post, we explain answer the question “what is a trivial copy assignment operator in C++”.
Table of Contents
What are classes and objects in C++?
Classes are defined in C++ using keyword class
followed by the name of the class. Classes are the blueprint for the objects, and they are user-defined data types that we can use in our program. Objects are an instantiation of a class. In C++ programming, most of the commands are associated with classes and objects, along with their attributes and methods. Here is a simple class example:
1 2 3 4 5 6 7 |
class Tmyclass { public: std::string str; }; |
Now we can create our objects with this Type of myclass
as shown below.
1 2 3 |
Tmyclass o1, o2; |
What is a copy assignment operator in C++?
The Copy Assignment Operator in a class is a non-template non-static member function that is declared with the “operator=“. When you create a class or a type that is copy assignable (that you can copy with the = operator symbol), it must have a public copy assignment operator. Here is a simple syntax for the forced (defaulted) copy assignment operator with default option;
Syntax (Since C++11),
1 2 3 |
class_name & class_name :: operator= ( const class_name& ) = default; |
here is an example in a class,
1 2 3 |
Tmyclass& operator=( const Tmyclass& other) = default; // Default Copy Assignment Operator |
This default copy assignment operator is declared automatically in a new class declaration, it is implicitly-defined or defaulted copy assignment operator and also a trivial copy assignment operator.
What is a trivial copy assignment operator in C++?
The trivial copy assignment operator is default operator in any class declarations. This means you don’t need to declare it as above, let’s give examples without using it.
Let’s give a simple C++ example to copy assignment operator with default
option, here is a simple class
1 2 3 4 5 6 7 8 |
class Tmyclass { public: std::string str; }; |
this is same as below,
1 2 3 4 5 6 7 8 9 10 |
class Tmyclass { public: std::string str; Tmyclass& operator=( const Tmyclass& other) = default; // Default Copy Assignment Operator }; |
As you see both are examples are same at runtime, because this is default in any class declaration and it is automatically declared. And here is how you can use this “=” copy assignment operator on the objects of one of these given class examples,
1 2 3 4 5 |
Tmyclass o1, o2; o2 = o1; // Using Trivial Copy Assignment Operator |
The trivial copy assignment operator is a default copy assignment operator that copies the given class object to a new class object. It has the same mechanics as the std::memmove method. All C language data types are trivially copy-assignable, which means the trivial copy operator is compatible with them.
When you create a simple class it has a trivial copy assignment operator.
It is trivial if the class has;
- no user-defined copy assignment operator,
- no virtual member methods or functions,
- no virtual base classes,
- the copy assignment operator selected for every direct base of this class, or every non-static class type, or every array of class type is trivial
Is there a simple example of a trivial copy assignment operator in C++?
After these useful information above, let’s give an example to trivial copy assignment operator. Lets assume that, we TmyclassA
as a base class and we have a new TmyclassB
class.
1 2 3 4 5 6 7 |
class TmyclassA { public: std::string str; }; |
This new class can use the copy assignment operator from the TmyclassA
as in the example below.
1 2 3 4 5 6 |
class TmyclassB : public TmyclassA { // Trivial Copy Assignment Operator is defined in base }; |
As you see, here, because of : public TmyclassA
part, this TmyclassB
class has trivial copy assignment operator from TmyclassA
. Now we can use this in the copy of class objects as below.
1 2 3 4 5 |
TmyclassB o1, o2; o2 = o1; // Using Trivial Copy Assignment Operator |
Is there a full example of a trivial copy assignment operator in C++?
Here is an example with a copy assignment operator in a class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
#include <iostream> #include <string> class TmyclassA { public: std::string str; }; class TmyclassB : public TmyclassA { // Trivial Copy Assignment Operator is implicitly defined }; int main() { TmyclassB o1, o2; o1.str = "LearnCplusplus.org"; o2 = o1; // Using Trivial Copy Assignment Operator std::cout << o1.str << std::endl; std::cout << o2.str << std::endl; system("pause"); return 0; } |
here is the output”
1 2 3 4 5 |
LearnCplusplus.org LearnCplusplus.org Press any key to continue . . . |
As you see, in Modern C++, we can simply use the trivial copy assignment operator in other sub classes in C++ without any declaration.
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.