2

I understand if I want a const array in a class namespace in C++ I cannot do:

class c
{
private:
  struct p
  {
    int a;
    int b;
  };
  static const p pp[2];
};

const c::p pp[2] =  { {1,1},{2,2} };

int main(void)
{
  class c;
  return 0;
}

I must do:

class c
{
public:
  struct p
  {
    int a;
    int b;
  };
  static const p pp[2];
};

const c::p pp[2] =  { {1,1},{2,2} };

int main(void)
{
  class c;
  return 0;
}

But this requires "p" and "pp" to be public, when I want them to be private. Is there no way in C++ to initialise private static arrays?

EDIT: ------------------- Thanks for the answers. In addition I want this class to be a library, header files only, for use by a main project. Including the following initialiser results in " multiple definition of " errors when included by multiple files.

const c::p c::pp[2] =  { {1,1},{2,2} };

How can I solve this?

Sam
  • 995
  • 2
  • 10
  • 19
  • In addition to the actual question answers, in C++ `(void)` is needless and bad practice, just like `class c` is nothing. You need to have `int main() { c varname; return 0; }` – Puppy Jan 05 '11 at 14:32
  • Thanks - why is (void) bad practice? – Sam Jan 05 '11 at 14:35
  • (void) doesn't do any harm, but it doesn't do any good either. It is supported for compatibility with C, but this may change in future version. – TonyK Jan 05 '11 at 15:49

3 Answers3

10

Your first code snippet works fine. You just need to change it to:

const c::p c::pp[2] =  { {1,1},{2,2} };
Björn Pollex
  • 75,346
  • 28
  • 201
  • 283
TonyK
  • 16,761
  • 4
  • 37
  • 72
2

Most of the time you should not have private static members and from the snippet I see this one is no exception.

Instead, you remove the struct from visibility altogether, putting it and the instance into the anonymous namespace of the compilation unit where your class functions are.

Users of the class then do not need to see implementation detail.

An exception would be where the struct or a private static member function needs access to the private members of the class. If that is the case you need to at least declare its existence as a friend in the class header so you lose nothing really by declaring it static once you have to show it is there anyway.

CashCow
  • 30,981
  • 5
  • 61
  • 92
  • I'm not sure I quite understand your response, but I think you're saying I should have these static members as local variables to the .cpp file where the functions are defined. However what I want to do (as you can see from the edit), is keep all definitions inside the header file, to allow the compiler to easily optimise. This class will be abstracted to a header file and the const arrays still need to be initialised. – Sam Jan 05 '11 at 14:50
  • 1
    Having them in a header file will not get the compiler to optimise but might make it take longer to compile. – CashCow Jan 05 '11 at 15:36
0

You need to qualify pp with c:: as in

const c::p c::pp[2] = { {1,1},{2,2} };

Otherwise you're trying to define a new array to the global scope instead of initializing the member.

kbjorklu
  • 1,338
  • 8
  • 10