Six functions in C++

  • 2020-06-03 07:54:11
  • OfStack

1. Constructor

In C++, the constructor is the first of six functions. When an object is created, it is a life-to-death process during its entire cycle, that is, 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 how the object is created. In C++, when you create an object you call the constructor to create the object. In the class, there is a default constructor, but you can also use the constructor to create the object and initialize the data. Take a look at this example:


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 initializes only one data member, and the other data members are random values. The second constructor initializes two data members and the rest are random values. The third constructor is to initialize all data members.

2. Destructor

In C++, the constructor is the creation of an object, and the destructor is the death of the object from birth to death. Destructors can also reclaim allocated memory space during object destruction.


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, as shown above, has no type, no arguments, no return value, and if there is no allocation of open memory space when constructing the object

Like the default destructor, destructor 1, if memory is allocated, the allocated memory must be reclaimed when an object is destructed, otherwise memory leaks will occur.

3. Copy the constructor

In C++, if there is a memory request operation in the constructor and a copy of the object appears in other functions, then the constructor needs to be copied.


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;
}; 
int main()
{
  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, and if there is no copy constructor, the default is used

Copy the constructor, then it is a shallow copy, then the destructor repeated memory release error. Then you need to make a deep copy

Operation, rewriting the copy constructor to copy assignment to an object. And when you write a copy constructor, the argument must be" & "The reference passes, otherwise

It's a syntax error.

4. Assignment function

In C++, the assignment function is the fourth function if there is a memory request operation in the constructor and two objects in other programs directly or indirectly

To perform an assignment, you need an assignment function.


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;
}

Because of the assignment of the object in the main function, if the "=" operator is not overdefined, the same memory will be freed twice

Operation error. In the "=" overload operation, if pointer operation is involved, you must determine whether two objects are the same object, namely the self-assignment operation, otherwise

An error can occur when an operation to release a pointer occurs. Then use delete to free up the original memory resources, otherwise you will cause a memory leak.

5. Addressing functions for 1 general objects

In C++, the addressing function for general object 1 is the fifth function.


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

The addressable function of a general object in 1 is to return the address of the object directly.

6. An addressing function for a constant object

In C++, the addressing function for a constant object is the sixth function.


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

The address function in a constant object also returns the address of the constant object directly.


Related articles: