当前位置:网站首页>Structure and Deconstruction

Structure and Deconstruction

2021-05-04 14:42:28 Cunninghamia lanceolata

c++_ Structure and destruction

Constructors

 A constructor is a special function 

It is mainly used to initialize objects when creating objects ,  It is the member variable of the object with initial value . 

You can also write the logic that the object needs to execute at the beginning of creation in the constructor
Constructors :
  1. The function name of the constructor is the same as the class name
  2. Constructor has no function return value ( No return value type )
  3. Constructor can overload
When to call the constructor :
  1. Called automatically when an object is generated ( Define the object , Dynamic memory allocation )

    class MyNode
    {

     int id;
     char* name;
    public:
     MyNode(int a) {
      MyNode::id = a;
      MyNode::name = nullptr;
      printf(" To call the parameterized constructor :id:%d, name:%d\n", MyNode::id, MyNode::name);
     }
     MyNode() {
      MyNode::id = 0;
      MyNode::name = nullptr;
      printf(" To call the parameterless constructor :id:%d, name:%d\n", MyNode::id, MyNode::name);
     }
    };

    int main()
    {
     //  Parameterless construction calls automatically
     MyNode MuShan;
     MyNode* muShan = new MyNode;
     //  Parameterized construction calls automatically
     MyNode mushan(10);
     MyNode* Mushan = new MyNode(10);
     int a; // Add a breakpoint - Invalid code
     return 0;
    }
Features of constructors :
  1. If there is no explicit constructor in a class , The system will automatically assign a default ( Implicit ) Nothing
 //  example 
 class CA{
     int a;
 public:
     void setA();
 };//  There is no constructor here
 int main()
 
{
     CA a; //  At this point, the system automatically calls a hidden invisible ,  Nothing
 }
 //  The hidden constructor looks like this --> 
 // CA(){}
  1. If there are multiple constructors in the class , So there are usually different parameter lists and function bodies

  2. If the user has already provided a class with no parameters ( Ginseng ) structure , Then the system will no longer provide a default construct

  3. If there are only parameter constructions in class , But there's no parameterless construction , Because the system no longer provides a default construct , The class will have to be defined with parameters , Otherwise, an error will be reported

Copy structure

 Copy construction is a special kind of constructor with parameters ,  Parameters are objects of the same type ,  Initialization for objects of the same type 
Basic usage :
int main()
{
 int a = 10;
 int b(a);
 cout << b;
}
The characteristics of copy construction :
  1. If you don't write copy constructs , The system will provide an implicit copy construct , This copy structure will assign the data members in the parameter object one by one to its own data members

b The value of and a identical , But there are also problems (a Members of the s and b Members of the s Point to the same address , If a.s change b.s It's going to change )

  1. Custom copy construction
  Class name  (const  Class name &  Reference name  ) { }

Shallow copy ( The default copy structure is a kind of invisible shallow copy of system automatic call )

MyStu::MyStu(const MyStu& other)
{
 id = other.id;
 name = other.name;
}

Deep copy : ( Can solve the default copy structure, two pointers point to the same memory )

MyStu::MyStu(const MyStu& other) //  Deep copy 
{
 id = other.id;
 if (other.name == nullptr)
  name = nullptr;
 else
 {
  int length = strlen(other.name);
  name = new char[length + 1];
  strcpy(name, other.name);
 }
 name = other.name;
}
  1. There is only one copy construct , There is no function overload
The call to the copy construct :
  1. Explicit copy construction calls ;

  2. Implicit copy construction calls ;

 int main()
 
{
  MyStu m;
  MyStu m1(m);  // Explicit calls to copy constructs
  MyStu m2 = m;  // An implicit call to a copy construct
  MyStu m3;
  m3 = m;    // Overload assignment
  return 0;
 }
When to call a copy construct :
  1. When a function passes parameters , The formal parameter of a function is a class object

  2. If the return value type of a function is a class type , At the end of a function call , When returning an object

 MyStu fun(MuStu s) {return s;}
 //  Two copy construct calls occur

Destructor

 Destructors are also special constructors 
The main function is to do some cleanup at the end of the object declaration cycle
Write the last thing in the object life cycle in the destructor

Constructors :

  1. Function name and class name are the same , Add before function name ~
  2. No return value type , And there's no parameter list
  3. If the class doesn't write its own destructor , The system automatically provides an implicit deconstruction that does nothing

When to call destructor :

  1. Called automatically when the object dies ( End of object scope , Dynamic memory is released )
  2. Destructors can be actively called through objects , A destructor must be a public property
class MyStu {
 int id;
 char* name;
public:
 ~MyStu();     //  Destructor
};

MyStu::~MyStu()     //  destructor ( Release functions that clean up class objects )
{
 if (name)
  delete[] name;
 name = nullptr;
}

版权声明
本文为[Cunninghamia lanceolata]所创,转载请带上原文链接,感谢
https://chowdera.com/2021/05/20210504144133431B.html

随机推荐