在结构体初始化:
- 结构体默认public,类默认private,结构体可以像类一样初始化
- C++11标准后,可以再变量定义时直接初始化
#include<iostream>
#include<string>
struct TestStruct
{
unsigned char ucCmdID;
unsigned char ucCmdParam;
TestStruct()
:ucCmdID(100), ucCmdParam(20) {}
};
struct TestStruct1
{
unsigned char ucCmdID = 100;
unsigned char ucCmdParam = 20;
};
int main()
{
TestStruct scTestStruct;
std::cout << std::to_string(scTestStruct.ucCmdID) << std::endl;
std::cout << std::to_string(scTestStruct.ucCmdParam) << std::endl;
system("pause");
return 0;
}
结果: