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.