Explicit call of constructor and destructor

Source: Internet
Author: User

1. First, read the following code to explicitly call the Destructor:

C ++ code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# Include <iostream>
Using namespace STD;

Class myclass
{
Public:
Myclass ()
{
N _ = 1;
Cout <"Constructors" <Endl;
}
~ Myclass ()
{
Cout <"Destructors" <Endl;
}
Void display ()
{
Cout <n _ <Endl;
}
PRIVATE:
Int N _;
};

Int main (void)
{
Myclass * pmyclass = new myclass;
Pmyclass-> ~ Myclass ();
Delete pmyclass;
Return 0;
}

Output:

Constructors

Destructors

Destructors


Confirmed some arguments:

Two things were actually done in new,

First, call malloc to allocate the required memory (actually calling operator new), and second, call the constructor.

Two things were done during the delete operation,

One is to call the analysis function, and the other is to call free to release the memory (actually calling operator delete ).

This is only for demonstration. Normally, the Destructor is called only once. If it is called twice, the Destructor has a delete operation, this may cause two errors when the memory is released.

2. Then let's look at it again: explicitly calling the constructor (the first method ):

C ++ code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# Include <iostream>

Using namespace STD;

Class myclass
{

Public:

Myclass ()
{
N _ = 1;

Cout <"Constructors" <Endl;
}

~ Myclass ()
{
Cout <"Destructors" <Endl;
}

Void display ()
{
Cout <"n =" <n _ <Endl;
}

PRIVATE:

Int N _;

};

Int main (void)
{

Myclass * pmyclass = (myclass *) malloc (sizeof (myclass ));

Pmyclass-> myclass: myclass (); // method 1

Pmyclass-> display ();

Free (pmyclass); // you cannot use delete. It corresponds to malloc and does not call destructor.

Return 0;
}

Output:

Constructors

N = 1


3. Display call Constructor (method 2): Placement new

C ++ code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# Include <iostream>

Using namespace STD;

Class myclass
{
Public:
Myclass ()
{
N _ = 1;
Cout <"Constructors" <Endl;
}

~ Myclass ()
{
Cout <"Destructors" <Endl;
}

Void display ()
{
Cout <"n =" <n _ <Endl;
}

PRIVATE:

Int N _;
};

Int main (void)
{
Char TMP [10];
Myclass * pmyclass = new (TMP) myclass; // placement new usage
Pmyclass-> display ();
Pmyclass-> ~ Myclass (); // It is not the heap memory and cannot be deleted.
Return 0;
}

Constructors

N = 1

Destructors

PlacementThe role of new is to create an object (call the constructor of this class) but create an object on an existing memory block without allocating memory. It can be used on objects that need to be created and deleted repeatedly to reduce the performance consumption of allocated and released memory.


Reference: http://www.cnblogs.com/fangyukuan/archive/2010/08/28/1811119.html

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.