static constant members for array size

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

MyClass.h

class MyClass
{
public:

static const int cTotalCars;

private:

int m_Cars[cTotalCars];
};

MyClass.cpp

相关标签:
5条回答
  • 2021-02-09 16:07

    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.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-02-09 16:11

    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 讨论(0)
  • 2021-02-09 16:12

    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];.

    0 讨论(0)
  • 2021-02-09 16:13

    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.

    0 讨论(0)
提交回复
热议问题