Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

static const class members

I have a variety of constants that I need to reference throughout my program. Rather than using global variables, I've been using static const class members:

class Human
{
public:
    static const int HANDS = 2;
    static const int FINGERS = 10;
};

The problem is that I need to read the value in from an XML data file. I know that I can initialize a static member with a function:

const int Human::HANDS = ReadDataFromFile();

Since the order of initialization can only be predicted in the same compilation unit, I have to define all of them in the same CPP file. That's not really a problem but it gets a bit cluttered.

The real problem is that everything in my ReadDataFromFile() function needs to be ready for use before my code even has a chance to run. For instance, I have an XML class that normally handles reading XML data from files. I can't use it in this case, though, because the static members are initialized before my XML class object is even constructed.

Aside from random global variables everywhere, is there a better solution to organize constants?

like image 529
user974967 Avatar asked Oct 15 '12 21:10

user974967


People also ask

Can static members be const?

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.

What is a static member of a class?

Static members are data members (variables) or methods that belong to a static or a non static class itself, rather than to objects of the class. Static members always remain the same, regardless of where and how they are used.

What is static const in C++?

“static const” is basically a combination of static(a storage specifier) and const(a type qualifier). The static determines the lifetime and visibility/accessibility of the variable.

Can we have a static const in C++?

An example of using static const member variables in C++ is shown below with common types (integer, array, object). Only one copy of such variable is created for its class. The variable cannot be modified (it is a constant) and is shared with all instances of this class.


2 Answers

You need to have your XML file read when you try to initialize the variable. However, you can get hold of it using a static object inside a function:

XMLData const& access_config_file() {
    static XMLData data = readXMLData();
    return data;
}

You can then reference access_config_file() from wherever you need to access it and pull the values out. The static variable gets initialized the first time the function is called.

like image 73
Dietmar Kühl Avatar answered Sep 17 '22 23:09

Dietmar Kühl


Make your XML class object a static member in this class too. i.e.,

class Human
{
public:
    static XMLReader x;
    static const int HANDS;
    static const int FINGERS;
};

Then in the implementation file, you provide the definitions of these static members, i.e.,

XMLReader Human::x();
const int Human::HANDS= x.ReadDataFromFile();
const int Human::FINGERS =x.ReadDataFromFile();
like image 41
Vivek Tyagi Avatar answered Sep 19 '22 23:09

Vivek Tyagi