0

I have a problem with a part of old C++ code. In the header file of the class myClass there is a static array member defined as follow:

static const int myArray[];

In the .cpp file it is initialized as follow:

const int myClass::myArray[]={2,4,5,7,9,11,13};

Is that allowed?

I don't care if it is not nice for the moment, I just want to know if it is going to give me weird problems..

3
  • 4
    Yes, it's perfectly ok Commented Sep 10, 2020 at 7:32
  • Better use const std::array<int,FIXED_SIZE> myArray; or const std::vecto<int> myArray;. This will save you a lot of time in debugging possible problems later. TThe existing parts of the code won't be too heavily impacted, in the best case you don't need to change anything. Just saying.` Commented Sep 10, 2020 at 7:42
  • 1
    Is that allowed? What are you afraid about? The empty brackets? In the .h file, it is a declaration only. Hence, other translation units don't need to know the size. In the .cpp file, the size is determined by initialization. Please, note: static member variables do not contribute to the class size. Hence, for instancing of that class, the actual size of the static member is not needed. Initialization in the .cpp file is necessary. Otherwise, the linker would complain. (Newer standards added static inline but you tagged c++98 where this still was far future.) Commented Sep 10, 2020 at 9:14

1 Answer 1

1

Is that allowed?

To repeat @bolovs comment:

Yes, it's perfectly ok

I'm not fully sure what especially OP worried about. So, I list the suspected issues the OP may have seen.

1. declaration in .h vs. definition in .cpp

That was the usual (and only) way to define static member variables in C++ – before static inline member variables were invented in C++17.

As a header might be included into more than one translation unit (aka. .cpp file), a definition in the header would be processed by the compiler multiple times and, hence, violate the One Definition Rule and usually result in a linker error.

2. declaration with empty brackets in .h

For the declaration, the actual size of the array is not necessary.

Please, note: static member variables does not contribute to the size of the class. Hence, when the class is used in other translation units then the unknown size of the array is nothing that hinders the compiler to determine the correct size of the class.

3. definition with empty brackets in .cpp

This is an ancient feature which was inherited from C: An array may be defined leaving the size out when it can be determined from the initializer.

This is useful to eliminate redundancy from code.

(Otherwise, annoying bugs like const char text[10] = "Hello world."; may result because the author was not able to count correctly that "Hello world." requires 13 elements but not 10.)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.