C++ 类中的static成员的初始化
生活随笔
收集整理的這篇文章主要介紹了
C++ 类中的static成员的初始化
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
#include <iostream>
using namespace std;
class Test
{
public:
? ? Test() : y(1), r(y), d(3){} //對于常量型成員變量和引用型成員變量,必須通過參數(shù)化列表的方式進行初始化。
? ? ~Test(){}
? ? int y; ? ? ?//普通變量成員
? ? int &r; ? ? //引用成員變量
? ? const int d; ? ?//常量成員變量
? ? static int c; ? //靜態(tài)成員變量
? ? static const int x = 2.1; ? //靜態(tài)常量整型成員變量
? ? static const int xx; ? ? ? ?//靜態(tài)常量整型成員變量聲明
? ? static const double z; ?//靜態(tài)常量非整型成員變量聲明
? ? static const float zz = 6.6; ? ?//靜態(tài)常量非整型成員變量
};
const int Test::xx = 4; //靜態(tài)常量整型成員變量定義
const double Test::z = 5.1; 靜態(tài)常量非整型成員變量定義
int Test::c = 2;
int main(void)
{
? ? cout << Test::x << endl; ? ?
? ? return 0;
}
using namespace std;
class Test
{
public:
? ? Test() : y(1), r(y), d(3){} //對于常量型成員變量和引用型成員變量,必須通過參數(shù)化列表的方式進行初始化。
? ? ~Test(){}
? ? int y; ? ? ?//普通變量成員
? ? int &r; ? ? //引用成員變量
? ? const int d; ? ?//常量成員變量
? ? static int c; ? //靜態(tài)成員變量
? ? static const int x = 2.1; ? //靜態(tài)常量整型成員變量
? ? static const int xx; ? ? ? ?//靜態(tài)常量整型成員變量聲明
? ? static const double z; ?//靜態(tài)常量非整型成員變量聲明
? ? static const float zz = 6.6; ? ?//靜態(tài)常量非整型成員變量
};
const int Test::xx = 4; //靜態(tài)常量整型成員變量定義
const double Test::z = 5.1; 靜態(tài)常量非整型成員變量定義
int Test::c = 2;
int main(void)
{
? ? cout << Test::x << endl; ? ?
? ? return 0;
}
總結(jié)
以上是生活随笔為你收集整理的C++ 类中的static成员的初始化的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 单列模式(懒汉)测试代码
- 下一篇: 快速理解桥接模式