C ++ constructor and destructor

Source: Internet
Author: User

C ++ constructor and destructor
Constructor:

Purpose:

1) Allocate space: allocate the storage space of non-static data members

2) initialize members: Initialize non-static data members.

Allocate space:

1) contains pointer variables and requires the programmer to apply for space explicitly (apply using new)

2) Non-pointer variable: space is automatically allocated by the system.

Initialize members:

1) Use the value assignment statement to initialize: A common variable.

2) use expression table initialization: Common variables + Const members, reference members, object members

Call time: when defining an object, the system automatically calls the constructor.

1) The constructor is automatically called when an object is created.

Coord p1 (1 );

Coord p2 = 1; // The constructor is also called at this time, but the premise is that the constructor can have only one parameter.

2) if the object is created with new, the constructor is automatically called.

Syntax: functions with the same name and class name, no return type, inline functions, overload functions, and default parameter values are allowed.

Code:

 

Class A {private: int x; int & rx; const float pi; public: A (int x1): rx (x), pi (3.14) // rx (x) equivalent to rx = x, pi (3.14) is equivalent to pi = 3.14 {x = x1; // common variable}; call: A a (10 );

 

 

Use of the member initialization table

Syntax:

Class Name: constructor name (parameter table): (data member name 1 (Initial Value 1), data member name 2 (Initial Value 2 ),...... )

{

Function body

}

Note:

1. class member initialization: Class Members are initialized in the order they declare in the class, regardless of their order in the member initialization table.

2. When data members contain arrays, they should be implemented through the value assignment statement in the constructor. They cannot be used to initialize tables.

Destructor: deal with the aftermath

Purpose: release space:

1) contains pointer variables, requiring the programmer to explicitly release space

2) Non-pointer variable: the system automatically releases space.

Call time:

1) when an object is revoked, such as an object defined in a function, the object is revoked when the function call ends.

2) if the object is created with new, The Destructor is automatically called when you delete it.

Syntax: no return type, no parameter, function name is added before class name "~ "

Code:

    class X      {      public:          X()  { }          ~X() { }      };   

Note:

1. Why should I define a destructor as a virtual function?
Because: virtual functions are designed to support polymorphism ,... Add later

Problems encountered

# Include
 
  
Using namespace std; class AAA {public: AAA (void );~ AAA (void) ;}; AAA: AAA () {}; int main () {AAA t; // error AAA * p = new AAA (); // system ("pause"); return 1 ;}
 

 

Problem Analysis: BecauseThe compiler declares a default constructor and destructor for each class, copying constructor and value assignment operator.. The system does not automatically generate classes when you define them. The constructor and destructor have been defined in the class, and the system does not generate them by itself.

AAA t; // Error

Cause: the object defined on the stack is that the system automatically calls the constructor and destructor. At the link, the system will find that the implementation of the Destructor cannot be found, and an error will be reported, indicating that the Destructor cannot be found. If you want to use it, but you cannot find it, an error will be reported.

AAA * p = new AAA (); // No error is reported

Cause: the constructor is called during the new operation. However, you need to manually release the constructor when releasing the constructor. Because there is no delete operation, the Destructor is not used. When linking a program, no destructor is used, so no error is reported.

Int main () {// AAA t; // error AAA * p = new AAA (); delete p; // error system ("pause"); return 1 ;}

In the preceding example

AAA * p = new AAA (); // No error is reported

Delete p;

In this case, the Destructor is required when the program executes the delete operation. The system will find its implementation. If no implementation is found, an error is returned.

Therefore, weIf you declare a function, you must remember to implement it, especially constructor and destructor. Copy the constructor and value assignment operator.


Related Article

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.