How to use the special member function in C++ class
This article introduces the relevant knowledge of "how to use special member functions in C++ class". In the operation process of actual cases, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!
1. Default constructor
Default constructors are constructors that can be initialized without parameters. There are two types of constructors: one with no parameters and one with default values for all parameters.
2. Copy constructor
Copy constructors are constructors that take objects of this class as arguments. Other parameters are possible, but default values must be provided.
3. Copy assignment operator
Overloading equals = assigns objects of this class to defined objects.
4. Destructor function
There's nothing to say.
5. Move constructor
C++11 added, the rvalue object of this class is the constructor of the parameter, and the rest is the same as copying the constructor.
6. Move Copy Operator
Same as copy assignment operator, except that argument is right value.
It is easy to confuse the definition, and the code will be clear:
#include #include class Foo {public: std::string s; //default constructor Foo() { std::cout