Six functions of C++

1. Constructor

   In C++, the constructor is the first of six functions, and when an object is created, it is a life-to-death life-cycle

Process i.e. the constructor creates the object and the destructor destructs the object. When an object is created, the constructor is called to create an object, which is the object's creation

build process. In C++, when you create an object, you need to call the constructor to create the object. In the class, there is a default constructor. Of course, you can also

To use constructors to create objects and initialize data. See the example below:

class Base 
{
   public:
       Base()
       {}
       Base(int a=0):num(a)
       {}
       Base(int a=0,int b=0):num(a),age(b)
       {}
       Base(int a=0,int b=0,double c=0.0):num(a),age(b),slaroy(c)
       {}
   private:
       int num;
       int age;
       double slaroy;
};

    In the above constructor, the constructor must be the same as the class name, the constructor is untyped, the first constructor is the default constructor,

The second constructor is to initialize only one data member, other data members are random values. The second constructor is for two data members

Initialize, other data members are random values. The third constructor initializes all data members.

2. Destructor function

   In C++, the constructor is when an object is created, and the destructor is the death process of the object from birth to death. At the same time the destructor is also

The allocated memory space can be reclaimed when the object is destructed.

class Base
{
   public:
       Base()
       {}
       Base()
       {
          p= new char[strlen("default")+1];
          strcpy(p,"default");
       }
       ~Base()
       {
           if(p != NULL)
           {
              delete[] p;
              p=NULL;
           }
       }
   private:
       char *p;
};

        The destructor is shown above. It has no type, no parameters, and no return value. If the space is not allocated when the object is constructed, then

The destructor is like the default destructor. If memory is allocated, when an object is destructed, the allocated memory is also processed.

Recycling, otherwise it will cause a memory leak.

Third, the copy constructor

  In C++, if there is an operation of requesting memory in the constructor, and the copy of the object occurs in other functions, then the copy constructor is required.

class Base
{
   public:
       Base()
       {}
       Base()
       {
          p= new char[strlen("default")+1];
          strcpy(p,"default");
       }
       Base(const Base &s)
       {
         p= new char[strlen(s.p)+1];
         strcpy(p,s.p);
       }
       ~Base()
       {
           if(p != NULL)
           {
              delete[] p;
              p=NULL;
           }
       }
   private:
       char *p;
};
intmain ()
{
   Base a;
   Base b(a);
   return 0;
}

       Since there is a copy assignment to the object in the main function, a copy constructor is required. If there is no copy constructor, the default one will be used.

Copy the constructor, then a shallow copy is performed at this time, then an error will occur that the destructor repeatedly releases the memory. Then a deep copy is required at this time.

Operation, rewrite the copy constructor to copy the assignment to the object. When writing a copy constructor, note that the parameter must be passed by reference with "&", otherwise

is a syntax error.

Fourth, the assignment function

   In C++, the assignment function is the fourth function. If there is an operation to apply for memory in the constructor, and there are two objects directly or indirectly in other programs

To perform an assignment operation, an assignment function is required.

class Base
{
   public:
       Base()
       {}
       Base()
       {
          p= new char[strlen("default")+1];
          strcpy(p,"default");
       }
       Base(const Base &s)
       {
         p= new char[strlen(s.p)+1];
         strcpy(p,s.p);
       }
       Base& operator=(const Base &s)
       {
           if(&s==this)
              return *this;
           delete[] p;
           p= new char[strlen(s.p)+1];
           strcpy(p,s.p);
           return *this;
        }
       ~Base()
       {
           if(p != NULL)
           {
              delete[] p;
              p=NULL;
           }
       }
   private:
       char *p;
}; 
int main()
{
   Base a,c;
   Base b(a);
   c=a;
   return 0;
}

      由于在主函数中进行了对象的赋值操作,如果没有对“=”运算符进行重载定义,则会产生两次释放同一个内存的

操作的错误。在“=”重载操作中,如果涉及到指针操作,则必须判断两个对象是否为同一个对象即自赋值操作,否则

当进行释放指针的操作时,就可能产生错误。然后要用delete释放原有的内存资源,否则将造成内存泄漏。

五、对一般对象的取址函数

  在C++中,对一般对象的取址函数为第五个函数。

class Base
{
   public:
       Base* operator&()
       {
          return this;
       }
}; 

  在一般对象的取址函数是直接返回该对象的地址,则为取其地址。

六、对常对象的取址函数

  在C++中,对常对象的取址函数为第六个函数。

class Base
{
   public:
       const Base* operator&() const
       {
          return this;
       }
};

   在常对象的取址函数也是直接返回该常对象的地址,则为取其地址。

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325382494&siteId=291194637