STL——静态常量整数成员在class内部直接初始化
如果class内含const static integral data member,那么根据C++标志规格,我们可以在class之内直接给予初值。所谓integral泛指所有的整数型别(包括浮点数),不单只是指int,下面是一个例子:
#include<iostream>
using namespace std; template <typename T>
class testClass
{
public:
static const double _datai=1.2;
static const long _datal=3L;
static const char _datac='c';
}; int main()
{
cout<<testClass<int>::_datai<<endl;
cout<<testClass<int>::_datal<<endl;
cout<<testClass<int>::_datac<<endl;
}
一般,非const的static数据成员是不能在类的内部初始化,但是,我们可以为静态成员提供const整数类型的类内初始值。
例如,下面的情况会报错:
#include<iostream>
using namespace std; template <typename T>
class testClass
{
public:
static double _datai=1.2;
static const long _datal=3L;
static const char _datac='c';
}; int main()
{
cout<<testClass<int>::_datai<<endl;
cout<<testClass<int>::_datal<<endl;
cout<<testClass<int>::_datac<<endl;
}
错误提示:
如果加了const 或者constexpr之后,就可以在类内进行初始化了。
对于static成员,如果在类的内部提供了一个初值,则成员在类外的定义不能再指定一个初始值了。例如:
#include<iostream>
using namespace std; template <typename T>
class testClass
{
public:
static const double _datai=1.2;
static const long _datal=3L;
static const char _datac='c';
}; template <typename T>
const double testClass<T>::_datai=8.8; int main()
{
cout<<testClass<int>::_datai<<endl;
cout<<testClass<int>::_datal<<endl;
cout<<testClass<int>::_datac<<endl;
}
错误提示:
下面的情况是允许的,直接在定义的时候提供初始值或者在类内提供初始值之后只在类外定义但不提供初始值。
#include<iostream>
using namespace std; template <typename T>
class testClass
{
public:
static const double _datai;
static const long _datal=3L;
static const char _datac='c';
}; template <typename T>
const double testClass<T>::_datai=8.8; int main()
{
cout<<testClass<int>::_datai<<endl;
cout<<testClass<int>::_datal<<endl;
cout<<testClass<int>::_datac<<endl;
}
或者
#include<iostream>
using namespace std; template <typename T>
class testClass
{
public:
static const double _datai=1.2;
static const long _datal=3L;
static const char _datac='c';
}; template <typename T>
const double testClass<T>::_datai; int main()
{
cout<<testClass<int>::_datai<<endl;
cout<<testClass<int>::_datal<<endl;
cout<<testClass<int>::_datac<<endl;
}
STL——静态常量整数成员在class内部直接初始化的更多相关文章
- 静态常量整数成员在class内部直接初始化
#include <vector> #include <deque> #include <algorithm> #include <iostream> ...
- Cocos2d-x 3.1.1 学习日志2--error:仅仅有静态常量整型数据成员才干够在类中初始化
今天遇到比較低端的一个问题,就是成员的初始化问题,编译器也无法验证,不同的编译器有些能过有些不能过,我也不知道为什么,总是我们以vs为准吧,以为我们用的环境就是它,话不多说.解决方式例如以下: ...
- java:构造方法:无参构造/有参构造 this static关键字 静态变量 静态方法 代码块 封装 静态常量。
/*构造方法是一种特殊的方法,专门用于构造/实例化对象,形式:[修饰符] 类名(){ }构造方法根据是否有参数分为无参构造和有参构造*/public class Dog { ...
- C#中的静态常量(const)和动态常量(static和readonly)用法和区别
C#中有两种常量类型,分别为readonly(运行时常量)与const(编译时常量),本文将就这两种类型的不同特性进行比较并说明各自的适用场景.工作原理 readonly为运行时常量,程序运行时进行赋 ...
- C#静态常量和动态常量的区别
C#拥有两种不同的常量:静态常量(compile-time constants)和动态常量(runtime constants).它们有不同的特性,错误的使用不仅会损失效率,还可能造成错误.相比之下 ...
- 【C# 基础概念】静态常量和动态常量的区别
C# 静态常量和动态常量的区别 C#中有两种常量类型分别为readonly(运行时常量)与const(编译时常量),readonly是变量的常量,const是字面量的常量本文将就这两种类型的不同特性进 ...
- 静态变量和成员变量的区别、final修饰特点、创建对象的内存图、静态内存图
静态变量和成员变量的区别* 静态变量也叫类变量 成员变量也叫对象变量* A:所属不同 * 静态变量属于类,所以也称为为类变量 * 成员变量属于对象,所以也称为实例变量(对象变量)* B:内存中位置不 ...
- 静态常量(static final)在class文件里是如何的呢?
近期写项目遇到一个问题,来回折腾了几次,最终探究清楚了.不废话.上样例. 背景:由于项目小,没有使用配置文件,全部静态常量都放在Config.java里面了 public class Config { ...
- Java学习笔记11---静态成员变量、静态代码块、成员变量及构造方法的初始化或调用顺序
当创建一个对象时,各种成员变量及构造方法的初始化或调用顺序是怎样的呢? (1).如果类尚未加载,则先初始化静态成员变量和静态代码块,再初始化成员变量,最后调用相应的构造方法: (2).如果类已经加载过 ...
随机推荐
- chromedriver release note
----------ChromeDriver v2.25 (2016-10-25)---------- Supports Chrome v53-55 Resolved issue 1547: Chro ...
- MongoDB 与传统关系型数据库mysql比较
与关系型数据库相比,MongoDB的优点: 转载自 http://blog.sina.com.cn/s/blog_966e430001019s8v.html①弱一致性(最终一致),更能保证用户的访问 ...
- 原生JS写Ajax的请求函数
一.JS原生ajax ajax:一种请求数据的方式,不需要刷新整个页面:ajax的技术核心是 XMLHttpRequest 对象:ajax 请求过程:创建 XMLHttpRequest 对象.连接服务 ...
- Windows Phone 学习笔记(一) 数据存储
独立存储设置IsolatedStorageSetting private IsolatedStorageSettings _appSettings; public MainPage() { Initi ...
- Oracle开始从Java运行时中移除JAR包
早在2012年8月,在Java平台首席架构师Mark Reinhold宣布模块化项目Jigsaw之后不久,JEP 162这一题为“准备模块化”的提案就指出,在Jigsaw项目中为模块化系统开发的代码不 ...
- unity3d中的http通信
转载 http://blog.csdn.net/mfc11/article/details/8188785的博客,如果侵权,请留言我及时删除! 前言 Unity3d 是一个跨平台的引擎,在移动互联网浪 ...
- JAVA存取对象属性时,如果开程多线程,记得对相关存取方法作原子化操作定义
最显著的应用当然是银行存款和取款,不要存在存取数字和实际发生不一样的情况. synchronized关键字. class BankAccount { private int balance = 100 ...
- c#:类 相关练习;
1. 2. int i = a.Length;//获取字符串的长度 a = a.ToLower();//将字符串中的大写英文字符转化为小写 a = a.ToUpper();//将字符串中的小写 ...
- javascript 路线整理
前端开发很重要,编写脚本也不容易. 总结我以前的前端学习经历,基本是一团乱麻:css+javascript是在大三自学的,当时自己做课程设计,逼着自己在一个月之内,写了一个半成品的j2ee网站.当时, ...
- LightOJ 1422 Halloween Costumes(记忆化搜索)
题意:给你n天分别要穿的衣服,可以套着穿,但是一旦脱下来就不能再穿了,问这n天要准备几件衣服. =============================================== ...