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?

3
  • 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; } Commented Jan 5, 2011 at 14:32
  • Thanks - why is (void) bad practice? Commented Jan 5, 2011 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. Commented Jan 5, 2011 at 15:49

3 Answers 3

10

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

const c::p c::pp[2] =  { {1,1},{2,2} };
Sign up to request clarification or add additional context in comments.

3 Comments

As a side note, his second code snippet will not work as intended. c::pp will remain uninitialized while a ::pp array is created and initialized.
Edited with another question :)
@Sam: you should create a new question, most people that might have read this question will not read the edit and you are thus lowering the chances to get an answer.
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.

2 Comments

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.
Having them in a header file will not get the compiler to optimise but might make it take longer to compile.
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.

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.