当前位置:网站首页>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 .
边栏推荐
- Undefined symbols for architecture i386 is related to third-party compiled static libraries
- (5) Matrix key
- 软件工程-个人作业-提问回顾与个人总结
- MySQL practice: 2 Table definition and SQL classification
- h5 localStorage
- The solution of installing opencv with setting in pycharm
- Deploy wiki system Wiki in kubesphere JS and enable Chinese full-text retrieval
- Discrete device ~ diode triode
- I Summary Preface
- Necessary protection ring for weak current detection
猜你喜欢

MySQL practice: 4 Operation of data

opencv學習筆記三

xxl-job配置告警邮件通知

Pychart connects to Damon database

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

Calculation of decoupling capacitance

HEVC学习之码流分析

Leetcode22 summary of types of questions brushing in 2002 (XII) and collection search

(4) Independent key

Vs2019-mfc setting edit control and static text font size
随机推荐
Necessary protection ring for weak current detection
Record the problem yaml file contains Chinese message 'GBK' error
Can the encrypted JS code and variable name be cracked and restored?
(5) Matrix key
鲸会务一站式智能会议系统帮助主办方实现数字化会议管理
Two ways to realize time format printing
73b2d wireless charging and receiving chip scheme
See which processes occupy specific ports and shut down
The difference between push-pull circuit drive and totem pole drive
Is it safe to open an account in flush,
Esp8266wifi module tutorial: punctual atom atk-esp8266 for network communication, single chip microcomputer and computer, single chip microcomputer and mobile phone to send data
Reflection example of ads2020 simulation signal
MySQL query time period
SOC的多核启动流程详解
h5 localStorage
MySQL practice: 1 Common database commands
Zlib static library compilation
STM32 project design: an e-reader making tutorial based on stm32f4
Introduction of laser drive circuit
swift 代码实现方法调用