当前位置:网站首页>Intra class data member initialization of static const and static constexpr
Intra class data member initialization of static const and static constexpr
2022-06-26 08:32:00 【Snowball~】
Reference resources :https://blog.csdn.net/qq_34801642/article/details/104948850
example
2.1 static Data member
#include
using namespace std;
class A
{
public:
// Static member of integer type
static bool b;
static char c;
static int i;
// Floating point data members
static float f;
static double d;// static int i1 = 1; // error : Members with in class initializers must be constants
// static double d1 = 3.14; // error : Members with in class initializers must be constants
};
bool A::b = true;
char A::c = ‘a’;
int A::i = 1;
float A::f = 1.5;
double A::d = 2.5;
int main()
{
cout << A::b << endl;
cout << A::c << endl;
cout << A::i << endl;
cout << A::f << endl;
cout << A::d << endl;
return 0;
}
summary :
General static Data members can only be declared inside a class , Define and initialize outside the class .
2.2 static const Data member
#include
using namespace std;
class A
{
public:
// Static member of integer type
static const bool b1;
static const char c1;
static const int i1;
// Floating point data members
static const float f1;
static const double d1;
// Static member of integer type
static const bool b2 = false;
static const char c2 = 'b';
static const int i2 = 2;
// Floating point data members
// static const float f2 = 3.5; // error :"const float" A member of a type cannot contain an in class initializer
// static const double d2 = 4.5; // error :"const double" A member of a type cannot contain an in class initializer
// char m1[i1];// error :i1 The constant of has not been initialized
char m2[i2];
};
const bool A::b1 = true;
const char A::c1 = ‘a’;
const int A::i1 = 1;
const float A::f1 = 1.5;
const double A::d1 = 2.5;
const bool A::b2;
const char A::c2;
const int A::i2;
int main()
{
cout << A::b1 << endl;
cout << A::c1 << endl;
cout << A::i1 << endl;
cout << A::f1 << endl;
cout << A::d1 << endl;
cout << “---------------------------------------” << endl;
cout << A::b2 << endl;
cout << A::c2 << endl;
cout << A::i2 << endl;
return 0;
}
summary :
static const Data members can be declared inside a class , Define and initialize outside the class . When declared inside a class ,static const The data member has not been initialized , Not really const.
static const Integer data members can be declared and initialized within a class , Define... Outside the class . When declared and initialized within a class ,static const Data members are real const.
If at compile time static const A data member can be replaced by its value ( For example, it indicates the number of arrays ), It does not need to be defined . If it cannot be replaced ( As a parameter ), Must contain a definition statement . It is recommended to define once outside the class regardless of whether it is replaceable or not .
if static const The data member is not an integer (bool、char、int、short、long etc. ), Cannot initialize within a class .
2.3 static constexpr Data member
#include
using namespace std;
class A
{
public:
// Static member of integer type
// static constexpr bool b1; // error :constexpr Static data member declaration requires an in class initializer
// static constexpr char c1; // error :constexpr Static data member declaration requires an in class initializer
// static constexpr int i1; // error :constexpr Static data member declaration requires an in class initializer
// Floating point data members
// static constexpr float f1; // error :constexpr Static data member declaration requires an in class initializer
// static constexpr double d1; // error :constexpr Static data member declaration requires an in class initializer
// Static member of integer type
static constexpr bool b2 = false;
static constexpr char c2 = 'b';
static constexpr int i2 = 2;
// Floating point data members
static constexpr float f2 = 3.5;
static constexpr double d2 = 4.5;
// char m1[i1]; // error :i1 The constant of has not been initialized
char m2[i2];
};
// constexpr bool A::b1 = true;
// constexpr char A::c1 = ‘a’;
// constexpr int A::i1 = 1;
// constexpr float A::f1 = 1.5;
// constexpr double A::d1 = 2.5;
constexpr bool A::b2;
constexpr char A::c2;
constexpr int A::i2;
constexpr float A::f2;
constexpr double A::d2;
int main()
{
cout << A::b2 << endl;
cout << A::c2 << endl;
cout << A::i2 << endl;
cout << A::f2 << endl;
cout << A::d2 << endl;
return 0;
}

summary :
static constexpr Data members must be declared and initialized within a class . When declared and initialized within a class ,static constexpr Data members are real const.
If at compile time static constexpr A data member can be replaced by its value ( For example, it indicates the number of arrays ), It does not need to be defined . If it cannot be replaced ( As a parameter ), Must contain a definition statement . It is recommended to define once outside the class regardless of whether it is replaceable or not .
if static constexpr Even if the data member is not an integer , You can also perform in class initialization .
边栏推荐
- Relationship extraction -- casrel
- Discrete device ~ resistance capacitance
- 鲸会务为活动现场提供数字化升级方案
- STM32 porting mpu6050/9250 DMP official library (motion_driver_6.12) modifying and porting DMP simple tutorial
- (1) Turn on the LED
- h5 localStorage
- Apple motherboard decoding chip, lightning Apple motherboard decoding I.C
- Realizing sequence annotation with transformers
- Crawler case 1: JS reversely obtains HD Wallpapers of minimalist Wallpapers
- 利用无线技术实现分散传感器信号远程集中控制
猜你喜欢

Ora-12514: tns: the listener currently does not recognize the service requested in the connection descriptor

HEVC学习之码流分析

51 MCU project design: Based on 51 MCU clock perpetual calendar

Transformers loading Roberta to implement sequence annotation task

STM32 project design: temperature, humidity and air quality alarm, sharing source code and PCB

STM32 project design: smart door lock PCB and source code based on stm32f1 (4 unlocking methods)

static const与static constexpr的类内数据成员初始化

2020-10-20

三菱PLC若想实现以太网无线通讯,需要具备哪些条件?

Discrete device ~ resistance capacitance
随机推荐
Recyclerview item gets the current position according to the X and Y coordinates
STM32 encountered problems using encoder module (library function version)
Microcontroller from entry to advanced
教你几招:30句哄女孩的“霸道”温馨话,不看后悔!
MySQL practice: 4 Operation of data
STM32 project design: an e-reader making tutorial based on stm32f4
Relevant knowledge of DRF
在同花顺开户证券安全吗,
2020-10-20
X-VLM多模态模型解读
Teach you a few tricks: 30 "overbearing" warm words to coax girls, don't look regret!
1. error using XPath to locate tag
MySQL query time period
你为什么会浮躁
Torch model to tensorflow
StarWar armor combined with scanning target location
Relationship extraction --tplinker
VS2005 compiles libcurl to normaliz Solution of Lib missing
Can the encrypted JS code and variable name be cracked and restored?
Stream analysis of hevc learning