Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9226425
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:49:37+00:00 2026-06-18T04:49:37+00:00

I’ve a class like the following: class Parameter { public: Parameter(); virtual ~Parameter(); bool

  • 0

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;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T04:49:39+00:00Added an answer on June 18, 2026 at 4:49 am

    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;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

public static bool CheckLogin(string Username, string Password, bool AutoLogin) { bool LoginSuccessful; // Trim
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am using JSon response to parse title,date content and thumbnail images and place
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I would like to run a str_replace or preg_replace which looks for certain words
I am doing a simple coin flipping experiment for class that involves flipping a
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.