Static const double in c++

2019-02-09 03:14发布

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.

标签: c++ static
7条回答
Anthone
2楼-- · 2019-02-09 03:37

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.

查看更多
Rolldiameter
3楼-- · 2019-02-09 03:38

Because 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). 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.

查看更多
叛逆
4楼-- · 2019-02-09 03:46

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.:

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).

查看更多
来,给爷笑一个
5楼-- · 2019-02-09 03:51

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.

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.

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

查看更多
戒情不戒烟
6楼-- · 2019-02-09 04:00

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

ifndef SHAPE_H
#define SHAPE_H

class Shape
{
public:

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

#endif

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

查看更多
再贱就再见
7楼-- · 2019-02-09 04:02

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.

public static intfindFromLast (List <Double> l, double value ) {///…}
查看更多
登录 后发表回答