Thread: defining macros or static variables

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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.

  2. #2
    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.

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