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

  • Home
  • SEARCH
  • 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 6586569
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:48:27+00:00 2026-05-25T16:48:27+00:00

I am working on a program for my CS class. It is a simulation

  • 0

I am working on a program for my CS class. It is a simulation of a delivery company’s activities at an airport.

This is a very simple, small program consisting of a few header and source files and a main.cpp source file that orchestrates the simulation.

There are certain given constant values, such as the frequency of shipment arrival, the load capacity of planes, the amount of time it takes a worker to process certain items, etc. (all are integer values). It is necessary for me to access these variables throughout several functions in main.cpp

It seemed reasonable to declare these above the main() function as const ints, effectively making them global, e.g.

const int kTotalTime = 2000;
const int kPlaneCapacity = 25;
int main(){//...program code}

I am aware that global variables are to be avoided in most situations, as there are no restrictions on where they can be called and/or modified, which can lead to accidentally breaking parts of the program which in turn may be difficult to debug, as well as causing compatibility issues for future code, etc.
However since these are read-only values of a primitive data type, which are used throughout the program, it seemed like a reasonable solution. Also, it makes an explicit statement about the purpose of the variables to anyone reading the code as well as to the compiler.

Questions: Is my logic flawed? How so? When are global variables (const or not) reasonable to use? If this is a bad solution, then how would you suggest declaring constant read-only values such as these?

Thank you very much for your time!

  • 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-05-25T16:48:27+00:00Added an answer on May 25, 2026 at 4:48 pm

    Regarding the size and purpose of your program (as I understand it from your description) it probably doesn’t matter, but since it has an educational context, I’d suggest to "do it right".

    In such a situation I would go for a Config struct (or class, if you want to make it a bit smarter, see below) which carries the configuration values and can be tossed around your program. It has the advantage that you can easily change it if you have to, say, fetch your options from a file or from the command line.

    As for the class versus struct thingy (note that I am making a logical distinction here, not a technical). Either you just put all values as members in your struct and pass around const refs of it, or you make it a full fledged class with accessors that hide where the data is coming from (and how it is generated). Programming is always decision making and this is your decision to make. If you think you will have to allow more configuration possibilities in the future (like mentioned above) you may want to go for class abstraction.

    Yet another option is to scatter your data across your program, which is actually a lot smarter than it sounds. If every class knows only its configuration options (and hides them) you can actually make use of the OOP language, you’re using. Example:

    // footype.h
    class FooType {
      private:
        static const int fooOption;
    };
    // bartype.h
    class BarType {
      private:
        static const float barOption;
    };
    

    The question is, how to initialise this. One way could be to create a config.cpp that looks like this:

    #include "footype.h"
    #include "bartype.h"
    
    const int FooType::fooOption = 42;
    const float BarType::barOption = 7.4;
    

    So you have information hiding, and you still have all the config options together at one place (config.cpp).

    Edit:

    If you have config option that is required by many (more than one) different modules, you can go for a bit of sophistication (with indirection) like so:

    // footype.h
    class FooType {
      private:
        static const int& fooOption;
        static const bool& dumpLevel;
    };
    // bartype.h
    class BarType {
      private:
        static const float& barOption;
        static const bool& dumpLevel;
    };
    

    config.cpp:

    #include "footype.h"
    #include "bartype.h"
    
    static const int opt_foo = 42;
    static const float opt_bar = 7.4;
    static const bool opt_dumpLevel = false;
    
    const int& FooType::fooOption = opt_foo;
    const bool& FooType::dumpLevel = opt_dumpLevel;
    const float& BarType::barOption = opt_bar;
    const bool& BarType::dumpLevel = opt_dumpLevel;
    

    You can even make the options non-const if you want (but I don’t see the point in a configuration option that is mutable).

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

Sidebar

Related Questions

I'm a beginner in C# and I'm working on this program consisting of 4
I'm working on a simple lex program for class, and in it I'm creating
I'm working on a program for class that takes in a number from 0
I'm working on a program for my C++ programming class, using wxWidgets. I'm having
is there any small working program for recieving from and sending data to client
I'm working on a program for class that involves solving the Chinese Postman problem
Please have a glance at this program: class CopyCon { public: char *name; CopyCon()
I am working on a program that is a projectile motion simulation with a
I'm trying to make a working program from this example: http://msdn.microsoft.com/en-us/library/ms741870.aspx . I think
Why is this code not working? public class A { public Dictionary<int, string> dic

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.