当前位置:网站首页>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 .
边栏推荐
- Analysis of Yolo series principle
- Use a switch to control the lighting and extinguishing of LEP lamp
- Transformers loading Roberta to implement sequence annotation task
- Detailed explanation of SOC multi-core startup process
- 73b2d wireless charging and receiving chip scheme
- (4) Independent key
- STM32 porting mpu6050/9250 DMP official library (motion_driver_6.12) modifying and porting DMP simple tutorial
- optee中支持的时间函数
- golang json unsupported value: NaN 处理
- What is Qi certification Qi certification process
猜你喜欢
![[postgraduate entrance examination: planning group] clarify the relationship among memory, main memory, CPU, etc](/img/c2/d1432ab6021ee9da310103cc42beb3.jpg)
[postgraduate entrance examination: planning group] clarify the relationship among memory, main memory, CPU, etc

73b2d wireless charging and receiving chip scheme

SOC wireless charging scheme

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

Crawler case 1: JS reversely obtains HD Wallpapers of minimalist Wallpapers

Installation of jupyter

Analysis of internal circuit of operational amplifier

MySQL practice: 4 Operation of data

What is Qi certification Qi certification process

Recovering the system with Clonezilla USB disk
随机推荐
Application of wireless charging receiving chip xs016 coffee mixing cup
[postgraduate entrance examination planning group] conversion between signed and unsigned numbers
Using transformers of hugging face to realize text classification
I Summary Preface
1GHz active probe DIY
Delete dictionary from list
Text to SQL model ----irnet
golang json unsupported value: NaN 处理
xxl-job配置告警邮件通知
ZLMediaKit推流拉流测试
FFmpeg音视频播放器实现
XXL job configuration alarm email notification
Using transformers of hugging face to realize multi label text classification
批量修改文件名
在 KubeSphere 部署 Wiki 系统 wiki.js 并启用中文全文检索
Opencv learning notes II
Idea uses regular expressions for global substitution
鲸会务为活动现场提供数字化升级方案
VS2005 project call free() compiled with static libcurl library reported heap error
(vs2019 MFC connects to MySQL) make a simple login interface (detailed)