简体   繁体   English

C ++中的静态常量双

[英]Static const double in c++

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.然后在扩展 Shape 的类中,我使用 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.我将 const double Shape::pi = 3.14... 移动到 Shape.cpp 文件,然后我的程序进行编译。 Why does that happen?为什么会这样? thanks.谢谢。

If you have a way to add C++11 (or later) flag to your compiler, you would've been able to do:如果您有办法向编译器添加C++11 (或更高版本)标志,您将能够执行以下操作:

ifndef SHAPE_H
#define SHAPE_H

class Shape
{
public:

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

#endif

Since C++11 you are able to use const expressions to types other than integral ones.C++11您可以将 const 表达式用于整数类型以外的类型。 This enables you to declare and define in place your constant variable.这使您能够就地声明和定义常量变量。

Further details: https://en.cppreference.com/w/cpp/language/constexpr更多详情: https : //en.cppreference.com/w/cpp/language/constexpr

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_PI from <math.h> , which you should also get from <cmath> .另外,我建议使用来自<math.h> M_PI ,您也应该从<cmath>获得它。

Because const double Shape::pi = 3.14159265;因为const double Shape::pi = 3.14159265; is the definition of Shape::pi and C++ only allows a single definition of a symbol (called the one-definition-rule which you may see in it's acronym form ODR).Shape::pi的定义,而 C++ 只允许对符号进行单一定义(称为单定义规则,您可能会在它的首字母缩略词形式 ODR 中看到)。 When the definition is in the header file, each translation unit gets it's own definition which breaks that rule.当定义在头文件中时,每个翻译单元都会获得自己的定义,这违反了该规则。

By moving it into the source file, you get only a single definition.通过将其移动到源文件中,您只会得到一个定义。

It happens because you can't define Shape::pi more than once.发生这种情况是因为您不能多次定义 Shape::pi。 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.当您在 Shape.cpp 中包含 Shape.h 时定义一次,然后每隔一次在另一个 cpp 文件中使用 Shape.h 时定义一次。 When you go to link you program together the linker will barf because of multiple definitions.当你将你的程序链接在一起时,链接器会因为多个定义而失败。

The line const double Shape::pi = 3.14159265;线const double Shape::pi = 3.14159265; should be in your Shape.cpp file.应该在你的 Shape.cpp 文件中。 The header file is for declaring the variables.头文件用于声明变量。 You can only define a variable once, therefore it must be done in the .cpp .您只能定义一次变量,因此必须在.cpp完成。 The header file says how to use these variables and functions, the cpp file says what to do.头文件说明如何使用这些变量和函数,cpp 文件说明要做什么。

For primitive data types (like int, double but not char[]) you may also define the constant within the class definition within the header file, eg:对于原始数据类型(如 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 数据类型(但是某些编译器会允许它)。

Implement a function that returns the index of a value to the list if it exists.实现一个函数,该函数将值的索引返回到列表(如果存在)。 Otherwise return -1 if there is no value.否则,如果没有值则返回 -1。 If the same value exists more than once on the list then the first value is deleted from the bottom.如果同一个值在列表中多次出现,那么第一个值将从底部删除。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM