Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/129.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 为什么类内初始值设定项中不允许使用double_C++_Oop - Fatal编程技术网

C++ 为什么类内初始值设定项中不允许使用double

C++ 为什么类内初始值设定项中不允许使用double,c++,oop,C++,Oop,我一直在读Herb Shutter的“Exception C++”,第1项:#定义或常量和内联[…] 据说类内初始化只允许整数类型(整数、字符、布尔)和常量 我只是想知道为什么不能在类声明中初始化double/float。 有什么具体原因吗 class EngineeringConstants { // this goes in the class private: // header file static const doub

我一直在读Herb Shutter的“Exception C++”,第1项:#定义或常量和内联[…]

据说类内初始化只允许整数类型(整数、字符、布尔)和常量

我只是想知道为什么不能在类声明中初始化double/float。 有什么具体原因吗

class EngineeringConstants {      // this goes in the class
 private:                          // header file
  static const double FUDGE_FACTOR;
  ...
 };
 // this goes in the class implementation file
 const double EngineeringConstants::FUDGE_FACTOR = 1.35;
我只想知道为什么不允许以下声明:

class EngineeringConstants {      // this goes in the class
 private:                          // header file
  static const double FUDGE_FACTOR = 1.35;
  ...
 };

此语句已过时:在C++03中,不支持在类定义中使用
double
s进行初始化。在C++中(从2011修订开始),可以初始化类定义中的任意成员。此外,初始化不仅限于
静态
成员,还可以初始化非
静态
成员:

struct foo {
    static constexpr double value = 1.23;
    std::string   str = "foo";
};

禁止在C++03中使用浮点数初始化
静态
成员的历史原因是编译期间的数值可能不同于执行期间的数值。例如,当使用IEEE浮点在平台上交叉编译并使用IBM HEX浮标来定位平台时,即使对于在两个数字系统中都可表示的常量也会产生不同的结果。

@计算机:当前的官方C++标准是CITITS/ISO/IEC 1488—2011。早期版本可能还在使用,但这并不意味着标准的当前版本应该以有趣的方式引用。@纳瓦兹:我认为它确实有害,因为它暗示C++ 11是不同于C++的,而不是。C++显然不是C++ 03,我们将限定过时版本而不是当前版本!这是“C++”的选择而不是“C++ 11”损坏的答案实际上是故意的。@ dieMuanguHL: C++ 11也意味着版本,C++没有。它是如何危害的?@计算机C++意味着当前的标准,否则它意味着什么?如果您想指定一个较旧的标准,那么您可以明确地说明它(C++98、C++03、C++11,一旦C++14问世……)@Nawaz:如果我们一致地标记所有版本,并且不将当前版本单独列为有点奇怪,那么可能不会有什么害处。但是,C++ C++ C++ 11(例如,有一个C++ 11标签,但是没有C++ 03或C++ 98标签),这是错误的和有害的,因为它意味着C++ 11是不同于C++的东西!