Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Initialize static variables in C++ class?

I have noticed that some of my functions in a class are actually not accessing the object, so I made them static. Then the compiler told me that all variables they access must also be static – well, quite understandable so far. I have a bunch of string variables such as

string RE_ANY = "([^\\n]*)"; string RE_ANY_RELUCTANT = "([^\\n]*?)"; 

and so on in the class. I have then made them all static const because they never change. However, my program only compiles if I move them out of the class: Otherwise, MSVC++2010 complains "Only static constant integral variables may be initialized within a class".

Well that's unfortunate. Is there a workaround? I would like to leave them inside the class they belong to.

like image 331
Felix Dombek Avatar asked Feb 16 '11 17:02

Felix Dombek


People also ask

Can you initialize static variables in C?

How to work with static variables in C. Inside a function, you can initialize a static variable using the static keyword. I said “inside a function”, because global variables are static by default, so there's no need to add the keyword.

Can we initialize static variable?

You can define a static field using the static keyword. If you declare a static variable in a class, if you haven't initialized it, just like with instance variables compiler initializes these with default values in the default constructor. Yes, you can also initialize these values using the constructor.

How static variables are defined and initialized in C?

When static keyword is used, variable or data members or functions can not be modified again. It is allocated for the lifetime of program. Static functions can be called directly by using class name.

How do you declare a static variable in class?

We can define class members static using static keyword. When we declare a member of a class as static it means no matter how many objects of the class are created, there is only one copy of the static member. A static member is shared by all objects of the class.


2 Answers

They can't be initialised inside the class, but they can be initialised outside the class, in a source file:

// inside the class class Thing {     static string RE_ANY;     static string RE_ANY_RELUCTANT; };  // in the source file string Thing::RE_ANY = "([^\\n]*)"; string Thing::RE_ANY_RELUCTANT = "([^\\n]*?)"; 

Update

I've just noticed the first line of your question - you don't want to make those functions static, you want to make them const. Making them static means that they are no longer associated with an object (so they can't access any non-static members), and making the data static means it will be shared with all objects of this type. This may well not be what you want. Making them const simply means that they can't modify any members, but can still access them.

like image 176
Mike Seymour Avatar answered Oct 01 '22 01:10

Mike Seymour


Mike Seymour has given you the right answer, but to add...
C++ lets you declare and define in your class body only static const integral types, as the compiler tells. So you can actually do:

class Foo {     static const int someInt = 1;     static const short someShort = 2;     // etc. }; 

And you can't do that with any other type, in that cases you should define them in your .cpp file.

like image 44
Santiago V. Avatar answered Oct 01 '22 03:10

Santiago V.