C++ constructor assignment function overloading operator, etc.

I. Introduction

      The special functions of c++ classes are really loved and hated, constructors, copy functions, move construction, assignment functions, overloaded operators, etc...
      While giving us a lot of room for manipulation, the learning curve is really steep . Record your daily notes here, and we will continue to add class-related functions in the future. Work hard, young people!

Two, the text

Reference:
When C++'s copy constructor is called
Four default functions of C++ (constructor, destructor, copy function, assignment function)

1. Copy constructor

asrModel(const asrModel& other);
auto asr_model = std::make_shared<asrModel>(*this);

Fired when a class object is used to initialize another object.

2. Assignment function

Reference: Assignment functions in c++ classes

InferReqWrap& operator=(const InferReqWrap& infer) {  //赋值函数
    if (this != &infer) {
      _request = infer._request;
      _id = infer._id;
      _callbackQueue = infer._callbackQueue;
    }
    return *this;
  }

      The main purpose is to rewrite operator=()the function, and judge whether the objects are equal in the function. If they are not equal, the member variables in the class are reassigned, and then the reference of the class object is returned.

3, operator () () overloaded () operator

Reference:
C++ class operator () overloading and
the meaning of function object void operator () ()

void ConnectionHandler::operator()() {
    
     }

The meaning of operator()():

1、第一个括号代表我要重载运算符()

2、第二个括号代表重载时传入的参数,可以有参数,也可以不传入参数
效果:当实例化类的时候,会自动执行重载()方法中的逻辑。和构造函数的区别如下:

What is a functor in C++

1)首先是定义形式:
构造函数无返回值,而operator是可以有返回值的;
(2)定义时,构造函数需要类名,而重载operator()则不用;
(3)其次是调用形式:构造函数是声明对象,而仿函数则需要声明好的对象进行调用。
仿函数:
仿函数的意思是:它不是函数(其实是个类),但用法和函数一样。既然是个类,就可以存储很多变量和其他的信息,然后实现纯函数实现不了的功能。所以在一些需要函数作为参数的地方可以用仿函数代替。在STL里很多地方用到了仿函数。

4. The template in front of the method in the class

Reference: Detailed Explanation of C++ Class Templates

      The definition and use of class templates are similar to function templates. Sometimes, there are two or more classes with the same function but different data types. We can achieve compatibility of multiple data types by declaring template classes.
When a subclass inherits from a template class, it needs to let the compiler know what the data type of the parent class is.

1.父类一般类,子类是模板类, 和普通继承的玩法类似
2.子类是一般类,父类是模板类,继承时必须在子类里实例化父类的类型参数
3.父类和子类都时模板类时,子类的虚拟的类型可以传递到父类中

5. The way of class instantiation

Reference:
The use of four smart pointers in C++ and the difference between
class pointers and smart pointers

(1) Call constructor instantiation and pointer instantiation

//调用类的构造函数,返回类对象
C obj1("o1", 11, 111);
obj1.memberFunction1();
//使用new实例化类,返回指针
C *obj2 = new C("O2", 22, 222.0);
obj2->memberFunction1();

(2) The difference between class object and class pointer

ⅰ. Memory space and lifetime are different

      The types of the two determine their different distributions in memory. One is the object type. The memory space has been allocated for the object when it is created. The memory stack is used, which is a local temporary variable. The scope is in the function body and will be released when the function ends. One is a pointer type, which uses a memory heap, which is a permanent variable. When calling, you need to use new to allocate dynamic memory first, and you must manually delete it after use. You can also use smart pointers if you don't want to do it manually delete.

ii. Differences in calling methods

Objects use " . “operator invocation, and pointers use ” -> "operator invocation.

iii. The difference between access and delivery

      The object can be directly accessed, but polymorphism cannot be achieved. The declaration calls the constructor (memory has been allocated); when passing the object as a parameter, the copy constructor is called to copy the entire object space, and the parameter transfer takes up a lot of resources.
      The pointer variable is accessed indirectly, but it can realize polymorphism (the subclass object can be called through the parent class pointer, and the parent class virtual function rewritten in the subobject), and the constructor is not called.

(3) The difference between class pointers and smart pointers

ⅰ. The object will automatically steal the destructor to release memory after the end of its life cycle. The class pointer needs to be destroyed deleteby destructor, but the pointer to the ordinary data will not be automatically destroyed, which may lead to C++ memory leaks.
ⅱ. The smart pointer will automatically release the memory space at the end of the function, and there is no need to manually release the memory space, thus avoiding memory leaks
ⅲ. Smart pointer member function

成员函数:
use_count 返回引用计数的个数
unique 返回是否是独占所有权( use_count 为 1)
swap 交换两个 shared_ptr 对象(即交换所拥有的对象)
reset 放弃内部对象的所有权或拥有对象的变更, 会引起原有对象的引用计数的减少
get 返回内部对象(指针)   ---->可以把智能指针转换为类指针

(4) Conversion of class pointers and smart pointers

test* pTest = new test();
std::shared_ptr<test> ptr_test = std::shared_ptr<test>(pTest); //普通指针转shared_ptr
 
std::shared_ptr<test> ptr_test2 = std::make_shared<test>();
test* pTest2 = ptr_test2.get(); //shared_ptr转普通指针

3. Postscript

      c++The complexity of the class is indeed relatively high. When I think of phpthe shuttle when I was writing , I can goonly say that it is you cpp! c++Second spring. However, in the past few years, when the application of various high-level languages ​​​​was popular, c++it was obviously unable to keep up with the pace of rapid iteration. It can only be said that one generation of versions is one generation of gods.

end

Guess you like

Origin blog.csdn.net/LJFPHP/article/details/128195596