Joel Bodenmann
Joel Bodenmann

Reputation: 2282

Access C++ static member from another class

I've a class like the following:

class Parameter {
public:
    Parameter();
    virtual ~Parameter();

    bool parse();

    static int WindowWidth;
    static int WindowHeight;
    ....
};

Now, in my main I go with:

int main(int argc, char *argv[]) {
    Parameter parameter;
    Controller controller;

    parameter.parse("parameter.ini");
    controller.initialise();

    return 0;
}

Now, in the Controller object I'd need to access the static members of the Parameter class. Therefore, I'm just doing:

Controller::Controller() {
    m_numberOfSweepers = Parameter::NumberOfSweepers;
    m_ticks = Parameter::NumberOfTicks;
    m_window_cx = Parameter::WindowWidth;
    m_window_cy = Parameter::WindowHeight;
}

Everything compiles fine. Taking out GDB, I can see that within the parser method of theparameter object, the correct values are inside the static members. But within the Controller::Controller() method, I just get garbage.

What am I doing wrong?

Thanks in advance.

EDIT: In the cpp file of the Parameter class, I do the following (outside of any method):

int Parameter::WindowWidth;
int Parameter::WindowHeight;

Upvotes: 2

Views: 2743

Answers (2)

Appy
Appy

Reputation: 66

Your order of initialization is wrong. One possible way is, in main do:

Parameter parameter;
parameter.parse("parameter.ini");

Controller controller;
controller.initialise();

Upvotes: 1

Robert Kelly
Robert Kelly

Reputation: 1006

The problem is just order of initialization. Here, you construct the controller:

int main(int argc, char *argv[]) {
  Parameter parameter;
  Controller controller;

When this happens, the constructor for controller is called, doing the following:

Controller::Controller() {
  m_numberOfSweepers = Parameter::NumberOfSweepers; // uninitialized garbage
  m_ticks = Parameter::NumberOfTicks; // uninitialized garbage
  m_window_cx = Parameter::WindowWidth; // uninitialized garbage
  m_window_cy = Parameter::WindowHeight; // uninitialized garbage
}

Then you initialize parameter by parsing the ini file. But controller got these values before you did that. Unless Controller is storing references to the static members, it is just going to keep those garbage values.

A simple fix would be moving the code from your constructor into your Controller's 'initialise' method. For example,

Controller::Controller() : m_numberOfSweepers(0)
                         , m_ticks(0)
                         , m_window_cx(0)
                         , m_window_cy(0)
{
  // now empty
}

void Controller::initialise()
{
  m_numberOfSweepers = Parameter::NumberOfSweepers;
  m_ticks = Parameter::NumberOfTicks;
  m_window_cx = Parameter::WindowWidth;
  m_window_cy = Paramter::WindowHeight;
}

That would fix the current problem, assuming you call it in the proper order. But I think this design is maybe a little messy. Why do you need a seperate class for these parameters?

If you want to stick with it, maybe try to give it a less general name, like ControllerParameters or something? Anyways, good luck. Have a nice day!

EDIT It might also serve you well to initialize those statics, just so they aren't total garbage. Garbage is bad. Just say something like:

int Parameter::WindowWidth = 0;
int Parameter::WindowHeight = 0;

Upvotes: 1

Related Questions