Typical Declaration Of A Copy Assignment Operator Without std::swap
Object-Oriented Programming (OOP) is a method of mapping real-world objects and data to computer functions and data structures. Classes and Objects are part of object-oriented methods and typically provide features such as properties and methods. One of the features of an OOP IDE is the copy assignment operator that is used with “operator=” to create a new object from an existing one. In this post, we explain what the typical declaration of copy assignment operator is along with some C++ examples.
What are classes and objects in C++?
Classes are defined in C++ using the 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, because it is designed to be strongly object oriented most of the commands are associated with classes and objects, along with their attributes and methods. Here is a simple class example below.
class myclass { public: std::string str; };
|
What is 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 typical declaration of a copy assignment operator when the copy and swap idiom is not used,
Syntax.
class_name & class_name :: operator= ( const class_name& )
|
Here is an example in a class.
Tmyclass& operator=( const Tmyclass& other) // Using Copy Assingment Operator { if (this != &other) // not a self-assignment { // Copy or swap things } return *this; }
|
What is typical declaration of a copy assignment operator without std::swap?
Let’s give a simple C++ example of the typical declaration of a copy assignment operator with std::swap
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
class Tmyclass { public: std::string str;
Tmyclass& operator=( const Tmyclass& other) // Using Copy Assingment Operator { if (this != &other) // not a self-assignment { std::cout << “Copy Assignment Operator is called”<< std::endl; str = other.str; } return *this; } };
|
And here is how you can use this “=” copy assignment operator:
Tmyclass o1, o2;
o2 = o1; // Using Copy Assingment Operator
|
Is there a full example of the declaration of a copy assignment operator without std::swap in C++?
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 31 32 33 34 35 |
#include #include
class Tmyclass { public: std::string str;
Tmyclass& operator=( const Tmyclass& other) // Using Copy Assingment Operator { std::cout << “Copy Assignment Operator is called”<< std::endl; if (this != &other) // not a self-assignment { str = other.str; } return *this; } };
int main() { Tmyclass o1, o2;
o1.str = “LearnCplusplus.org”;
o2 = o1; // Using Copy Assingment Operator
std::cout << o1.str << std::endl; std::cout << o2.str << std::endl;
system(“pause”); return 0; }
|
Here is the output.
Copy Assignment Operator is called LearnCplusplus.org LearnCplusplus.org Press any key to continue . . .
|
As you see, in Modern C++, we can specialize the “=” operator with what to copy or not, with the copy assignment operator.
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.