C ++中的静态常量双

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2777541/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 00:38:39  来源:igfitidea点击:

Static const double in c++

c++static

提问by Crystal

Is this the proper way to use a static const variable? In my top level class (Shape)

这是使用静态常量变量的正确方法吗?在我的顶级班级(形状)

#ifndef SHAPE_H
#define SHAPE_H

class Shape
{
public:

    static const double pi;
private:
    double originX;
    double originY;
};

const double Shape::pi = 3.14159265;

#endif

And then later in a class that extends Shape, I use Shape::pi. I get a linker error. I moved the const double Shape::pi = 3.14... to the Shape.cpp file and my program then compiles. Why does that happen? thanks.

然后在扩展 Shape 的类中,我使用 Shape::pi。我收到链接器错误。我将 const double Shape::pi = 3.14... 移动到 Shape.cpp 文件,然后我的程序进行编译。为什么会这样?谢谢。

回答by Potatoswatter

Static floating-point data members must be defined and initialized in a source file. The one-definition rule forbids a definition outside the class {}block in the header, and only integral data members are allowed to be initialized inside the class {}block.

静态浮点数据成员必须在源文件中定义和初始化。一定义规则禁止class {}在头中的块外定义,并且只允许在class {}块内初始化整型数据成员。

This is also unfortunate because, being an algebraic value, having the immediate value on hand could be nice for optimization, rather than loading from a global variable. (The difference is likely to be inconsequential, though.)

这也是不幸的,因为作为一个代数值,手头的立即值可能有利于优化,而不是从全局变量加载。(不过,这种差异可能无关紧要。)

There is a solution, though!

不过有解决办法!

class Shape
{
public:
    static double pi()
        { return 3.14159265; }

private:
    double originX;
    double originY;
};

Inline function definitions, including static ones, are allowed inside the class{}block.

class{}块内允许内联函数定义,包括静态函数定义。

Also, I recommend using M_PIfrom <math.h>, which you should also get from <cmath>.

另外,我建议使用M_PIfrom <math.h>,您也应该从<cmath>.

回答by R Samuel Klatchko

Because const double Shape::pi = 3.14159265;is the definition of Shape::piand C++ only allows a single definition of a symbol (called the one-definition-rulewhich you may see in it's acronym form ODR). When the definition is in the header file, each translation unit gets it's own definition which breaks that rule.

因为C++const double Shape::pi = 3.14159265;的定义Shape::pi只允许一个符号的单一定义(称为单一定义规则,您可能会在它的首字母缩略词形式 ODR 中看到)。当定义在头文件中时,每个翻译单元都会获得自己的定义,这违反了该规则。

By moving it into the source file, you get only a single definition.

通过将其移动到源文件中,您只会得到一个定义。

回答by Benjamin

If you have a way to add C++0xflag to your compiler, you would've been able to do:

如果您有办法向C++0x编译器添加标志,则可以执行以下操作:

ifndef SHAPE_H
#define SHAPE_H

class Shape
{
public:

    static constexpr double pi = 3.14159265;
private:
    double originX;
    double originY;
};

#endif

In C++0xyou are able to use const expressions to types other than integral ones. This enables you to declare and define in place your constant variable.

C++0x你能够使用常量表达式来比积分之外的其它类型。这使您能够就地声明和定义常量变量。

回答by Joseph Lisee

It happens because you can't define Shape::pi more than once. It's defined once when you include Shape.h in Shape.cpp, and then again every other time you use Shape.h in another cpp file. When you go to link you program together the linker will barf because of multiple definitions.

发生这种情况是因为您不能多次定义 Shape::pi。当您在 Shape.cpp 中包含 Shape.h 时定义一次,然后每隔一次在另一个 cpp 文件中使用 Shape.h 时定义一次。当你将你的程序链接在一起时,链接器会因为多个定义而失败。

回答by zmbush

The line const double Shape::pi = 3.14159265;should be in your Shape.cpp file. The header file is for declaring the variables. You can only define a variable once, therefore it must be done in the .cpp. The header file says how to use these variables and functions, the cpp file says what to do.

该行const double Shape::pi = 3.14159265;应该在您的 Shape.cpp 文件中。头文件用于声明变量。一个变量只能定义一次,因此必须在.cpp. 头文件说明如何使用这些变量和函数,cpp 文件说明要做什么。

回答by Oliver

For primitive data types (like int, double but not char[]) you may also define the constant within the class definition within the header file, e.g.:

对于原始数据类型(如 int、double 但不是 char[]),您还可以在头文件中的类定义中定义常量,例如:

class Shape
{
public:
    static const double pi = 3.14159265;

private:
    double originX;
    double originY;
};

This will allow better compiler optimisation.

这将允许更好的编译器优化。

Edit:As Dennis pointed out below this is only allowed for integral types and not for double or float data types (however some compilers will allow it).

编辑:正如丹尼斯在下面指出的那样,这仅适用于整数类型,而不适用于 double 或 float 数据类型(但是某些编译器会允许它)。

回答by besarta saraqini

Implement a function that returns the index of a value to the list if it exists. Otherwise return -1 if there is no value. If the same value exists more than once on the list then the first value is deleted from the bottom.

实现一个函数,该函数将值的索引返回到列表(如果存在)。否则,如果没有值则返回 -1。如果同一个值在列表中多次出现,那么第一个值将从底部删除。

public static intfindFromLast (List <Double> l, double value ) {///…}