Home > Back-end >  Class c can't when defined initialization??????? Why can execute the following code
Class c can't when defined initialization??????? Why can execute the following code

Time:09-25

# define _CRT_SECURE_NO_WARNINGS
# include & lt; iostream>
using namespace std;

Class A
{
Public:

Int c=3;
int a=1;
Int b=2;
};
Int main (int arg c, char * argv [])
{
CoutA, A.

Cout & lt;
return 0;
}


12
123
Please press any key to continue...
Class did not allocate space during the definition???????
Why sizeof (A) would be 12 bytes

Can't class when defined initialization???????
Cout & lt; CodePudding user response:

now can only say that some of the c + + compiler is more and more intelligent, he will put your class initializes the members of the statements in the default initialization function complete (you can see in the disassembly code),

Class did not allocate space during the definition???????
Why sizeof (A) would be 12 bytes

this is more easy to explain, sizeof is not really A function, it is A macro, it evaluated for any type of variable is done at compile time rather than execution, that is to say in execution, size_t x=sizeof (A) has become A size_t x=12 such constants assignment statements,

CodePudding user response:

C + + 11 features
Non - static data member initializers
Sizeof got the corresponding type footprint size,

CodePudding user response:

Best to write code to initialize, encoder automatic initialization is uncertain

CodePudding user response:

Sizeof operator
  • Related