Search code examples
c++global-variablesconstants

Appropriate use of global const variables in C++?


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!


Solution

  • 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).