c++11 成员变量初始化
2016-10-31 20:24
239 查看
但是在C++11中,我们可以利用类内初始化:
可以看到基本类型不用多说,可以在类内初始化。在这里我们重点看const 与static,引用,以及数组。
const 的int 与float都能在类内初始化。但是static还是只有static const int 能在类内初始化,其他的static 还是能在类外初始化。
而引用则可以在类内初始化。而无论是const 数组还是非const 数组,都不能在类内显示初始化。
原贴:http://blog.csdn.net/u014120684/article/details/24853127
int j = 15; class Bclass { private: int f = 100; float g = 200.0; const float h = 30.0; const int a=10; // const int array[20]; // int thesecondarray[20] = { 0 }; int &b=j; int &k = f; static int c; static const int d=30; static const float e; public: Bclass() { // array[20] = { 0 }; 注释去掉有错误 //thesecondarray[20] = { 0 }; } void print() { cout << a << " " << b << " " << c << " " << d << " " << e << " " << f << " " << g << " " << h << " " << k << endl; //for (int i = 0; i < 20; ++i) //cout << array[i] << " "; //for (int i = 0; i < 20; ++i) //cout << thesecondarray[i] << " "; } }; int Bclass::c = 20; const float Bclass::e = 40.0;
可以看到基本类型不用多说,可以在类内初始化。在这里我们重点看const 与static,引用,以及数组。
const 的int 与float都能在类内初始化。但是static还是只有static const int 能在类内初始化,其他的static 还是能在类外初始化。
而引用则可以在类内初始化。而无论是const 数组还是非const 数组,都不能在类内显示初始化。
原贴:http://blog.csdn.net/u014120684/article/details/24853127
相关文章推荐
- C++11之初始化成员变量
- C++11中__func__,__cplusplus,long long类型,static_assert,noexcept,快速初始化成员变量用法
- c++11 中成员变量初始化的顺序
- c++11 中成员变量初始化的顺序
- const map 成员变量初始化例子
- 类的成员变量初始化
- objective-c 成员变量没初始化所造成的灾难
- static代码块、成员变量初始化、构造方法执行顺序
- C++ 类中特殊的成员变量(常变量、引用、静态)的初始化方法 --转
- Java 成员变量初始化
- 类的数组成员变量的初始化
- 成员变量和局部变量的初始化和内存运行机制分析
- C++中类成员变量的初始化以及函数变量初始化
- 类中有指针成员变量的初始化
- 指针成员变量需要初始化为nullptr吗?
- c++类中成员变量的初始化总结
- 使用初始化清单和直接在构造函数内初始化成员变量的区别
- 特殊成员变量的初始化
- 局部变量与成员变量的同名问题&显示初始化
- 类成员变量初始化专题