Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Initialize a static member ( an array) in C++

Tags:

c++

I intended to create a class which only have static members and static functions. One of the member variable is an array. Would it be possible to initialize it without using constructors? I am having lots of linking errors right now...

class A { public:   static char a[128];   static void do_something(); }; 

How would you initialize a[128]? Why can't I initialize a[128] by directly specifying its value like in C?

a[128] = {1,2,3,...}; 
like image 210
vtd-xml-author Avatar asked Apr 03 '10 04:04

vtd-xml-author


People also ask

How do you initialize a static member?

The initializer for a static data member is in the scope of the class declaring the member. A static data member can be of any type except for void or void qualified with const or volatile . You cannot declare a static data member as mutable . You can only have one definition of a static member in a program.


1 Answers

You can, just do this in your .cpp file:

char A::a[6] = {1,2,3,4,5,6}; 
like image 69
Brian R. Bondy Avatar answered Oct 13 '22 20:10

Brian R. Bondy