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 8682241
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T21:43:14+00:00 2026-06-12T21:43:14+00:00

I have a variety of constants that I need to reference throughout my program.

  • 0

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?

  • 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-12T21:43:16+00:00Added an answer on June 12, 2026 at 9:43 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a program that works with a variety of files on both the
I have a variety of anchor tags and input buttons that trigger a variety
I have a loop that runs through a variety of websites and I'd like
I have a table that describes a variety of objects in my system (ie.
I have a web application that supports a variety of clients on various platforms
I have a contact manager program that I'd like to design for multiple network
In my Rails application, I have a variety of database tables that contain user
I have a variety of image sets that I'm displaying on a web page
I have a variety of PHP scripts that use the mysqli suite of functions
I have a variety of Velocity template files that represent e-mail messages. I am

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.