static constant members for array size

前端 未结 5 801
一生所求
一生所求 2021-02-09 15:54

MyClass.h

class MyClass
{
public:

static const int cTotalCars;

private:

int m_Cars[cTotalCars];
};

MyClass.cpp

5条回答
  •  -上瘾入骨i
    2021-02-09 16:07

    If is was a static int you would need to place it into the .cpp file. You do not need the keyword static in this instance as all you want is a constant. Just use const int cTotalCars = 5;. It is better than #define as you have type info and also it has a symbol that can be viewed in a debugger.

提交回复
热议问题