1

I want to define array in class:

h:

class A
{
    protected:
        static const int TABLE[];
}

cpp:

const int A::TABLE[5] = {1, 2, 3, 4, 5};

This should be a const array all object of type class A will use.

  1. Should I define it in the header file? what is the best practice for this?

  2. I sew previous questions and some suggested to define it with "extern". like this:

h:

class A
{
protected:
extern const int TABLE[];
}

cpp:

const int A::TABLE[5] = {1, 2, 3, 4, 5};

it is insted of static?

............................

6
  • 1
    Either define it in exactly one source file or use inline in header inside class. Commented Dec 20, 2022 at 13:03
  • First of all, will the array be a separate member for each separate object instance of A, or should the array be shared by all object instances of A? That decides the use of static or not. Commented Dec 20, 2022 at 13:04
  • "what is the best practice for this..." Opinion based. Commented Dec 20, 2022 at 13:04
  • 1
    Unrelated, I'd also suggest std::array<int, 5> instead of int[5] Commented Dec 20, 2022 at 13:05
  • 3
    You cannot have an extern class member. Commented Dec 20, 2022 at 13:15

2 Answers 2

2

There are two options here. Either we can define it inside the class using the inline keyword with C++17 & onwards or declare it inside the class and then provide an out of class definition in exactly on source file.

Method 1

Here we use inline with C++17.

header.h

#pragma once 

class A
{
    protected:
//------vvvvvv---------------------------->inline used here with c++17
        inline static const int TABLE[] = {1, 2, 3, 4, 5};
};

Demo method 1


Method 2

Here we declare it inside the class and then define it outside the class in exactly one source file.

header

#pragma once 

class A
{
    protected:
        static const int TABLE[5];
};

source file

#include "header.h"

const int A::TABLE[5] = {1, 2, 3, 4, 5};

Demo method 2


Note that instead of using built in array I would recommend using std::array.

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

Comments

0

To clear up some of the confusion:

If you want to declare a class member variable which is NOT an instance variable (so, one per class, not one per instance of the class), in a header file and define it in a source file, you do it like this:

// In header file
class Foo {
    static Bar baz;
};
// In source file
Bar Foo::baz(args);

However, if you want to do the same thing for a variable that isn't a class member, you do it like this:

// In header file
extern Bar baz;

// In source file
Bar baz;

Note that in one case we use 'static' and in the other case we use 'extern'.

The word 'static' has multiple meanings in C++, and unfortunately this one is a bit confusing, but when appearing as part of a class variable declaration, it acts pretty much the same way as 'extern' does for an ordinary variable.

(I would have preferred that they use 'extern' rather than 'static' for declaring class member variables also, rather than adding yet another new meaning to 'static'. I think 'extern' for this usage would have been clearer, but that ship has long since sailed, so we have to deal with the language as it is, not as we would like it to be.)

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.