Thread: defining macros or static variables

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    630

    defining macros or static variables

    Hello

    I have to define default configuration settings for my application and I'm unsure which would be the best way to do this.

    Do you prefer macros:

    Code:
    #define DEFAULT_SOME_STRING "some string"
    #define DEFAULT_SOME_NUM_OPT 58
    over global variables:

    Code:
    static std::string default_some_str = "some string";
    static int default_some_num = 58;
    or there is some better way?

    I think the second option would be better for some reason, because if you have for instance double option, the user that would want to customize the program could clearly see that theres double/int/or some other data type required.

    as:
    Code:
    static double default_some_duble_opt = 2.3543;
    Thanks for help!

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    (static) constant global variables, because they're type-safe. And can be manipulated and interacted with, as well.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I'm not a great fan of #define for these sort of things, and you can use compiler defined constants here, so why not do that. As you mention, it gives type information as well.

    You may also want to make the value "const" if it's not meant to change.
    Code:
    static const std::string x = ...
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    630
    One more thing, should be global variables defined in .cpp file right? And then I just put extern word in front of each variable in a header file?

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, unless they're constants (const int, etc). In that case, the compiler can treat them as constant expressions and remove the variable altogether (kindof like #define).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by l2u View Post
    One more thing, should be global variables defined in .cpp file right? And then I just put extern word in front of each variable in a header file?
    Why do they need to be global? Generally, I've never needed to parse options [which I think is what you are doing] outside of a single .c[pp] file. You certainly can't mix static with extern, as they are (in this circumstance) complete opposites.

    A header file may declare a struct/class to store the option values and some functions to interface with the option parsing, but you should not need to know what the options are called or what their defaults are outside of the parser - unless you go about it in a fashion where you pass in a structure containing the relevant option information and then call a function to parse the options.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Also, if you go with global variables, make sure you put them into their own namespace (that's another thing that macros can't do) rather than polluting the global namespace.
    Another solution would be to put them into a class so you can use appropriate Set/Get methods to control access to the variables.

  8. #8
    Registered User
    Join Date
    May 2006
    Posts
    630
    So would it be better to define default options (static consts) inside a class where I store the options?

    Something like:

    Code:
    class options {
    private:
      static const double default_some_option = 5.2; 
      double m_some_option;
    };
    Or you think it would be better to make separate class for this or just define them outside the class?

    The other thing - should I name the static const the same way as other class member - beginning with m_ or are there any better rules for those data types?

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Since it's just a bunch of data, I'd suggest using a struct.

  10. #10
    Registered User
    Join Date
    May 2006
    Posts
    630
    Defining it as:

    Code:
    struct default_options {
      static const double some_option = 5.2;
    }
    and then access as:

    default_options::some_option ?

  11. #11
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Well these things have advantages and disadvantages. How would it be used?

    the struct is good if you want to fill it up with options and then pass it all at once to the set thing.

    the namespace is good if you only want one set of configurations.

    the OOP get/set method is good if you put it in a function.

    The overloaded constructor is good if you're using OOP.
    Last edited by robwhit; 08-04-2008 at 02:18 PM.

  12. #12
    Registered User
    Join Date
    May 2008
    Location
    Paris
    Posts
    248
    Since it's just a bunch of data, I'd suggest using a struct.
    The only difference between a class and a struct is that a class' members are private by default, and struct's members are public by default, as far as I know...

    Funny, today I discovered that with some old compilers, initialisation of a 'static const' is not possible. It should be done like this:
    Code:
    //.h file
    
    class MyClass {
        static const int a; // OK
        static const int a = 0; // error
    }
    Code:
    // .cpp file
    const int MyClass :: a = 0;
    but then again, this is with an old compiler: MS VisualStudio 6.

    I've just learned a while ago that you should be careful when you use static variables and functions: the linkage might fail because the order in which they are parsed is undefined. This problem appeared when I used a static function to initialise a static member (who were in the same namespace, though!).

  13. #13
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by MarkZWEERS View Post
    The only difference between a class and a struct is that a class' members are private by default, and struct's members are public by default, as far as I know...
    Right, but a class implies that you need abstraction, which you don't need if it's just a collection of data.

  14. #14
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    I usually manage configuration options with a singleton class. It uses a std::map to store options as name value std::string pairs. Clients call functions like get_int("NUMBER_OF_PARTICLES") or get_string("WINDOW_TITLE"), with any necessary conversion happening on the fly. You can set values the same way, and pass a text file to the constructor that will be read and parsed (name=value on separate lines).

    Not sure it's terribly flexible, but it seems to work for me.
    Last edited by medievalelks; 08-04-2008 at 05:20 PM.

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by MarkZWEERS View Post
    Funny, today I discovered that with some old compilers, initialisation of a 'static const' is not possible. It should be done like this:
    Code:
    //.h file
    
    class MyClass {
        static const int a; // OK
        static const int a = 0; // error
    }
    Code:
    // .cpp file
    const int MyClass :: a = 0;
    but then again, this is with an old compiler: MS VisualStudio 6.
    It is possible and valid
    MSVS6 is just too old
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. Static variables + Initialisation
    By kris.c in forum C Programming
    Replies: 2
    Last Post: 07-08-2007, 02:16 AM
  3. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  4. Replies: 5
    Last Post: 11-19-2002, 07:49 PM
  5. Replies: 2
    Last Post: 12-25-2001, 04:18 PM