当前位置:网站首页>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 .
边栏推荐
- 加密的JS代码,变量名能破解还原吗?
- Detailed explanation of self attention & transformer
- [已解决]setOnNavigationItemSelectedListener()被弃用
- h5 localStorage
- VS2005 project call free() compiled with static libcurl library reported heap error
- [postgraduate entrance examination planning group] conversion between signed and unsigned numbers
- Tokenizer description in Bert
- 51 MCU project design: Based on 51 MCU clock perpetual calendar
- 1. error using XPath to locate tag
- Oracle 19C local listener configuration error - no listener
猜你喜欢

RF filter
![[postgraduate entrance examination] group planning: interrupted](/img/ec/1f3dc0ac22e3a80d721303864d2337.jpg)
[postgraduate entrance examination] group planning: interrupted

STM32 porting mpu6050/9250 DMP official library (motion_driver_6.12) modifying and porting DMP simple tutorial

Recognize the interruption of 80s51

opencv學習筆記三

Introduction of laser drive circuit

鲸会务为活动现场提供数字化升级方案

MySQL query time period

MySQL practice: 4 Operation of data

Diode voltage doubling circuit
随机推荐
在 KubeSphere 部署 Wiki 系统 wiki.js 并启用中文全文检索
Discrete device ~ resistance capacitance
Idea automatically sets author information and date
Esp8266wifi module tutorial: punctual atom atk-esp8266 for network communication, single chip microcomputer and computer, single chip microcomputer and mobile phone to send data
Golang JSON unsupported value: Nan processing
opencv學習筆記三
STM32 encountered problems using encoder module (library function version)
加密的JS代码,变量名能破解还原吗?
Solve the problem that pychar's terminal cannot enter the venv environment
Deploy wiki system Wiki in kubesphere JS and enable Chinese full-text retrieval
Is it safe to open an account in flush,
Teach you a few tricks: 30 "overbearing" warm words to coax girls, don't look regret!
在同花顺开户证券安全吗,
Oracle 19C download installation steps
鲸会务一站式智能会议系统帮助主办方实现数字化会议管理
Time functions supported in optee
Software engineering - high cohesion and low coupling
CodeBlocks integrated Objective-C development
[postgraduate entrance examination planning group] conversion between signed and unsigned numbers
MySQL practice: 1 Common database commands