2
class School
{
    static const int *classcapacity; 
};

This expression is from my exam and it need to get initialized how can i do that ?

1
  • Remember that the pointer is not a constant so can be initialised and assigned normally. Here the const qualifier simply prevents the data pointed to from being modified through this pointer, i.e. *classcapacity = 0` is invalid. You do however need at least a static initialiser.
    – Clifford
    Commented Dec 7, 2009 at 17:00

4 Answers 4

8

You can initialize it in your source file, outside of the body of the class, just as you would any other static member variable, i.e.:

const int* School::classCapacity(new int(42));
0
2

Probably this way:

class School{
  static const int *classcapacity   ;
};
const int *School::classcapacity = 0;
2

If you want to initialize it with YOUR_INITIALIZER:

class School{ static const int *classcapacity ; } ;
const int* School::classcapacity = YOUR_INITIALIZER;
0

I know this is old, but in C++11, you'd write:

class School
{
   static const int* classcapacity{new int[4]}; // initialize with 4-element array
};
1
  • This is still invalid in C++11. Note that the member is non-const.
    – typ1232
    Commented Oct 18, 2015 at 21:53