C ++ struct Initialization

Source: Internet
Author: User

Today, when I was reading the structure of the MFC, I looked at the in-depth introduction to the MFC and found that there was such a line of code:

M_pmainwnd = new cmyframewnd;

At first glance, it's normal. Let's take a closer look. It seems that a pair of parentheses are missing in the new cmyframewnd. Strange! Then I flipped through the book and found that many of them were like this.

Isn't it possible for me to make a mistake? Generally, parentheses are added when a new class is added, and some initial values are put in it.

Test:

The point struct in Win32 is defined as follows:

Typedef struct tagpoint

{

Long X;

Long y;

} Point, * Ppoint, near * nppoint, far * lppoint;

Call method:

(1) Point * P = new point;

(2) Point * P = new point ();

For (1), under debug, you can see: p0x003ca130 {x = 0 xcdcdcdcd y = 0 xcdcdcdcd} tagpoint *,

The internal members of the manual point are not initialized.

For (2), under debug, we can see: p0x003ca130 {x = 0x00000000 y = 0x00000000} tagpoint *

The internal members of the manual point are initialized.

So I came up with a simple conclusion: when (1) is used, the compiler considers the code to be pure C code, so it is not the default constructor that calls the code, in this way, the initialization will not be performed, because C does not have the constructor, so no one has seen the second code style written in C. When (2) is used, the compiler considers the code as a pure C ++ code. Because c ++ has a class concept, it will automatically call the default constructor of the struct, initialize a member. Looks correct!

Test again:

Define a test struct in MFC

Typedef struct _ Test

{

Int M_a;

Cstring m_str;

} Test, * Ptest;

(1) test * P = new test;

(2) test * P = new test ();

I was disappointed by the results: (1) and (2) results:

P0x003adce0 {M_a = 0 xcdcdcdcd m_str = ""} _ test *

M_a is not initialized.

This is in conflict with what I mentioned above.

Why don't I initialize the built-in type after adding the class ???

No solution...

 

Please give me some answers.

 

 

 

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.