Static constant elements 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 does not work because it will say "expected constant expression" for the m_Cars array.

 class MyClass { public: static const int cTotalCars = 5; private: int m_Cars[cTotalCars]; }; 

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

+6
source share
5 answers

Static constant members of a simple type are an exception to this rule, so the last code is correct.

This exception is fairly modern (from C ++ 98, but not implemented by each compiler until several years later), so many old-fashioned teachers do not yet know about it. They prefer the idiom:

 class MyClass { public: enum { cTotalCars = 5 }; private: int m_Cars[cTotalCars]; }; 

This behaves exactly the same, but it makes little sense at present.

+7
source

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

Good thing you were asked to: define static members in CPP. Note that in the above code, the static member has no , even if the value is specified. The correct final code would look like this:

 // .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 space for the variable when using lvalue as the value. For a simple test that checks that a variable is not defined without this line, you can do:

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

Without a line in the .cpp file, the above code will throw a linker error indicating a missing MyClass::cTotalCars . The problem with the code is that it uses the static member const (as defined by use in the standard) and requires that the member be defined. Although the case of using a constant to determine the size of the array is not used.

+3
source

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

0
source

If the value was static int , you need to put it in a .cpp file. You do not need the static in this case, since all you want is a constant. Just use const int cTotalCars = 5; . This is better than #define , since you have type information, and there is also a symbol that can be viewed in the debugger.

0
source

It just can't work if you determine the size of the array in the cpp file. All class clients must know the size of the class instance, but they simply do not know about the .cpp file because you only put #include "MyClass.h" in the client files.

In other words, the definition of your class depends on the cpp file, which is not used when compiling files that use MyClass.

0
source

Source: https://habr.com/ru/post/898952/


All Articles