Deep understanding of construction and destructor functions

Source: Internet
Author: User


deep understanding of construction and destructor functions

 

I. Overview

Constructors and destructors are two concepts that you encounter when you first touch C + +, and are two concepts that are harder to grasp in C + + syntax. But they are learning C + + must master, can say without understanding constructs and destructor function, your C + + is not yet getting started.

This paper presents a systematic introduction to constructors and destructors so that those who have a preliminary understanding of these two concepts can have a further understanding.

second, what does the constructor do.

The constructor creates an object from scratch.

Constructors are like "initialization functions." It turns a bunch of random bits of memory into live objects. At least it initializes the domain used within the object. It can also allocate resources (memory, files, signals, sockets, and so on)

"ctor" is a typical acronym for Constructors (constructor).

 

Third, List x; and List x (); Is there a difference? ?

There's a very big difference.

Let's say the list is the name of a class. Then the function f () declares a local list object with the name x:

void F ()
{
List x; Local object named X (of Class List)
// ...
}

But function g () declares a function called X (), which returns a List:

 

void G ()
{
List x (); Function named X (that returns a List)
// ...
}

 

 

Four, Fred the default constructor for a class is always fred::fred () it.

No. A "default constructor" is a constructor that can be called without arguments. Therefore, a constructor with no parameters is, of course, the default constructor:

Class Fred {
Public
Fred (); Default constructor: Can be called without parameters
// ...
};

However, if the parameter is provided with a default value, then the default constructor with the parameter is also possible:

Class Fred {
Public
Fred (int i=3, int j=5); Default constructor: Can be called without parameters
// ...
};

 

 

v. When the establishment of a Fred an array of objects, which constructor will be invoked.

The default constructor for Fred (except for the following discussion).

You cannot tell the compiler to invoke a different constructor (except for the following discussion). If your Fred class does not have a default constructor, then trying to create an array of Fred objects will cause a compile-time error.

 

Class Fred {
Public
Fred (int i, int j);
// ... Suppose the Fred class does not have a default constructor ...
};

int main ()
{
Fred A[10]; Error: Fred class does not have a default constructor
fred* p = new FRED[10]; Error: Fred class does not have a default constructor
}

However, if you are creating a standard std::vector<fred> instead of the Fred Object array (since the array is harmful, then you might want to do so), you do not need a default constructor in the Fred class. Because you can give Std::vector a Fred object to initialize the element:

#include <vector>

int main ()
{
Std::vector<fred> A (Fred (5,7));
The 10 Fred objects in Std::vector will be initialized with Fred (5,7).
// ...
}

Although you should use std::vector instead of arrays, there are times when you should use arrays, so there is an "explicit initialization of arrays" syntax. It seems to be like this:

 

Class Fred {
Public
Fred (int i, int j);
// ... Suppose the Fred class does not have a default constructor ...
};

int main ()
{
Fred A[10] = {
Fred (5,7), Fred (5,7), Fred (5,7), Fred (5,7), Fred (5,7),
Fred (5,7), Fred (5,7), Fred (5,7), Fred (5,7), Fred (5,7)
};

10 Fred objects will be initialized with Fred (5,7).
// ...
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.