开发者

static constant members for array size

MyClass.h

class MyClass
{
public:

static const int cTotalCars;

private:

int m_Cars[cTotalCars];
};

MyClass.cpp

#include "MyClass.h"
const int MyClass::cTotalCars = 5;

The above code doesn't work because it will say "expected constant expression" for the 开发者_Python百科m_Cars array.

class MyClass
{
public:

static const int cTotalCars = 5;

private:

int m_Cars[cTotalCars];
};

The above will work, but I am told that I should always define static members in the CPP file, outside the class definition. What can I do?


Static const members of simple type are a exception to that rule, so you latter code is correct.

This exception is a fairly modern one (from C++98, but not implemented by every compiler until a few years later) so many old-fashioned teachers are not yet aware of it. They prefer the idiom:

class MyClass
{
public:
   enum { cTotalCars = 5 };

private:
    int m_Cars[cTotalCars];
};

That behaves exactly the same, but makes little sense nowadays.


The above will work, but I am told that I should always define static members in the CPP file, outside the class definition. What can I do?

Well, what you have been suggested to do: define the static members in the CPP. Note that in the code above the static member is not defined even if the value is stated. The proper final code would look like:

// .h (ignoring all but the static member)
class MyClass {
   static const int cTotalCars = 5;        // declaration and initialization
};
// .cpp
static const int MyClass::cTotalCars;      // definition (cannot have value!)

The definition in the .cpp file is what actually reserves the space for the variable when used as an lvalue. For a simple test that verifies that without that line the variable is not defined you can do:

void f( const int & x ) {}
int main() {
   f( MyClass::cTotalCars );
}

Without the line in the .cpp file the code above will trigger a linker error pointing to the missing definition of MyClass::cTotalCars. The problem with the code is that it uses the static const member (by the definition of use in the standard), and that requires the member to be defined. While the case of using the constant to define the array size does not constitute use.


I would rather use a #define C_TOTAL_CARS 5, then static const int cTotalCars = C_TOTAL_CARS; and then also, int m_Cars[C_TOTAL_CARS];.


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.


It just can't works if you define size of the array is set in cpp file. All class clients should know the size of class instance but they just have no idea about .cpp file because you only put #include "MyClass.h" in client-files.

In other words - your class definition is varies depending on the cpp-file which is not used while compile files that uses MyClass.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜