only static const integral data members can be initialized within a class

简介:

翻译一下:只有静态整型常量数据成员能在类中初始化

那么哪些数据属于整型呢,下面的都是

  • char
  • short
  • int
  • long
  • long long

所以只有以上这些类型的数据能在类中初始化,也就是你可以这样写

1  class  Test
2  {
3       static   const   int  i  =   100  ;
4  } ;
5 

 

但不能这样写,否则就会出现如标题所示的编译错误。

1  class  Test
2  {
3       static   const   float  i  =   100.0f  ;
4  } ;
5 

因为float不属于integral type,对于float类型可以在类中定义,在类外初始化,如下:

复制代码
class Test
{
    static const int i = 100 ;
    static const float f;
};

const float Test::f = 1.0f;
复制代码

 

另外要注意的是,bool也可以在类中初始化,像下面这样

1  class  Test
2  {
3       static   const   bool  flag  =   true  ;
4  } ;
5 

但是我却没有找到bool是integral type的说法,bool的值有两种,true和false,对应二进制的0和1,用一个bit就可以表示了,但是C++中长度最短的类型是char,所以只能用char表示,而char属于整型,是不是这样就把bool也归为整型了呢?猜测而已。

另外,C++标准对于哪些类型能在类中初始化有详细的描述,如下:

If a static data member is of const integral or const enumeration type, its declaration in the class definition can specify a constant-initializer which
shall be an integral constant expression

所以不只整型常量可以,枚举常量也可以在类中初始化,像下面这样

复制代码
 1  enum  COLOR
 2  {
 3      RED, 
 4      GREEN,
 5      BLUE
 6  } ;
 7 
 8  class  Test
 9  {
10       static   const  COLOR color  =  GREEN ;
11  } ;
复制代码

 

 本文转自zdd博客园博客,原文链接:http://www.cnblogs.com/graphics/archive/2010/03/04/1677991.html,如需转载请自行联系原作者

相关文章
【已解决】Error: Element type is invalid: expected a string (for built-in components) or a class/function
Error: Element type is invalid: expected a string (for built-in components) or a class/function
2571 0
【已解决】Error: Element type is invalid: expected a string (for built-in components) or a class/function
|
7月前
|
JavaScript
[Vue warn]_ Avoid using non-primitive value as key, use string_number value instea
[Vue warn]_ Avoid using non-primitive value as key, use string_number value instea
91 1
VS2005 error C2864 only static const integral data members can be initialized within a class
VS2005 error C2864 only static const integral data members can be initialized within a class
|
SQL Java 数据库连接
attempted to return null from a method with a primitive return type
attempted to return null from a method with a primitive return type
224 0
Dart报The return type ‘bool‘ isn‘t a ‘void‘, as required by the closure‘s context
Dart报The return type ‘bool‘ isn‘t a ‘void‘, as required by the closure‘s context
|
C++
warning: non-static data member initializers only available with -std=c++11 or -std=gnu++11
warning: non-static data member initializers only available with -std=c++11 or -std=gnu++11
201 0
|
JSON Go 数据格式
解决:interface conversion: interface {} is float64, not int
今天遇到一个小坑,但是自己陷进去好久,说起来有些不好意思,但是感觉还是应该拿出来晒一晒,希望大家别再被类似的问题耽误了。
1093 0
|
JavaScript
void mutating a prop directly since the value will be overwritten whenever the parent component re-renders. Instead, use a data or computed property b
void mutating a prop directly since the value will be overwritten whenever the parent component re-renders. Instead, use a data or computed property based on the prop's value. Prop being mutated: vue项目示例,请参考甄佰 单向数据流所有的 prop 都使得其父子 prop 之间形成了一个单向下行绑定:父级 prop 的更新会向下流动到子组件中,但是反过来则不行。
3422 0
delete attempted to return null from a method with a primitive return type (int)
今天被自己给蠢死了 今天在代码中遇到这个错误, 百度翻译一下:映射方法,从一org.system.mapper.child.chmorganizationexaminationmapper.delete返回零作为一个原始的方法的返回类型(int)。
2912 0