Static const double in c++

前端 未结 7 469
轮回少年
轮回少年 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:33

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

提交回复
热议问题