Static const double in c++

前端 未结 7 460
轮回少年
轮回少年 2021-01-02 07:54

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 cons         


        
7条回答
  •  执念已碎
    2021-01-02 08:32

    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.

提交回复
热议问题