CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14
  1. #1
    Join Date
    Aug 2006
    Posts
    144

    How to declare global variable across multiple files?

    Hi,

    I have multiple variables for which ones I have defaults. In java I would declare a static class like this

    Code:
    package com.xxxx.aaaa;
    
    public class Constants {
    	public final static int a = 1;
    	public final static int b = 2;
    
    }
    Now I can access the variable in all source files via Constants.a

    How would this look likein C++? In a header file I cannot initialize a variable. So even it is static I would have to create an instance and set all the member variables in the beginning. In the next souce file I would have to create again an instance, does this instance have the values automatically? Is that the way to go? What would be best?

    Thanks,
    J.

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: How to declare global variable across multiple files?

    You can write:
    Code:
    class Constants {
    public:
        static const int a = 1;
        static const int b = 2;
    };
    But it is something of a special case for static const member variables of integer types. Normally, you would write:
    Code:
    class Constants {
    public:
        static const double c;
    };
    then in a source file, you would write:
    Code:
    const double Constants::c = 1.5;
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Aug 2006
    Posts
    144

    Re: How to declare global variable across multiple files?

    but do I have to write in all 5 source files again and again

    Code:
    const double Constants::c = 1.5;
    ?

    I want to avoid is, if I have to change that default in the future I would have to search all files where I have set this value. I want one central place where to do it.

    thanks, j.

  4. #4
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: How to declare global variable across multiple files?

    Quote Originally Posted by joebar
    but do I have to write in all 5 source files again and again
    No, you do that in exactly one source file, otherwise you would be breaking the one definition rule.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  5. #5
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: How to declare global variable across multiple files?

    Quote Originally Posted by joebar View Post
    but do I have to write in all 5 source files again and again
    No. The proposed solution is more or less equivalent to the "C" approach of declaring the global "extern" in a header and then defining it in exactly one source file, except that it wraps this in a class.

  6. #6
    Join Date
    Aug 2006
    Posts
    144

    Re: How to declare global variable across multiple files?

    I have

    defaults.h

    Code:
    #ifndef DEFAULTS_H
    #define DEFAULTS_H
    
    using namespace std;
    
    class Defaults
    {
      public:
      static const double c;
    };
    #endif
    And in some cpp file I have

    Code:
    #include "defaults.h"
    
    using namespace std;
    
    //some more code here
    
    const double Defaults::c = 1.4;
    During a "make" I get

    invalid use of qualified-name ‘Defaults::c’
    What is wrong?

  7. #7
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: How to declare global variable across multiple files?

    It looks fine to me. What is the code that you commented out?

    By the way, you should not have that using directive in the header file, at least not at file scope.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  8. #8
    Join Date
    Aug 2006
    Posts
    144

    Re: How to declare global variable across multiple files?

    okay, crafted something really simple, luckily got the same error:

    Code:
    #include "mainwindow.h"
    #include "defaults.h"
    
    
    int main (int argc, char *argv[])
    {
    const double Defaults::c = 1.4;
    
      MainWindow* window = new MainWindow();
    
    }
    MainWindow is an empty class with an empty constructor.

  9. #9
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: How to declare global variable across multiple files?

    You should be defining that variable at file scope, not within a function.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  10. #10
    Join Date
    Aug 2006
    Posts
    144

    Re: How to declare global variable across multiple files?

    Thanks, solved. Gretings to my old home S'pore.

  11. #11
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: How to declare global variable across multiple files?

    Quote Originally Posted by joebar View Post
    Code:
      MainWindow* window = new MainWindow();
    
    }
    MainWindow is an empty class with an empty constructor.
    You should never allocate using new when you don't have to. You certainly don't if MainWindow is as you say. If you choose to allocate using new, you should always pair it with a delete----even in a trivial example program. There's no reason to get into the bad habit of not matching those up every time!

  12. #12
    Join Date
    Apr 1999
    Posts
    27,449

    Re: How to declare global variable across multiple files?

    Quote Originally Posted by joebar View Post
    Code:
    #include "mainwindow.h"
    #include "defaults.h"
    
    
    int main (int argc, char *argv[])
    {
    const double Defaults::c = 1.4;
    
      MainWindow* window = new MainWindow();
    
    }
    MainWindow is an empty class with an empty constructor.
    C++ isn't Java. The operator new in C++ is not the same as "new" in Java.

    Don't ever believe that they are the same, as that will cause all sorts of grief in your C++ program.
    Code:
    int main (int argc, char *argv[])
    {
      MainWindow window;
    }
    This created an object, without the memory leak.

    But basically, do not code C++ as if you're coding Java. Pretend that Java doesn't exist when you're writing a C++ program -- if you try to mimic Java in C++, you'll wind up with all sorts of problems.

    Regards,

    Paul McKenzie

  13. #13
    Join Date
    Mar 2010
    Location
    Melbourne Australia
    Posts
    454

    Re: How to declare global variable across multiple files?

    Java has got a garbage collector , so any object referenced using new keyword will get reallocated automatically , with C++ you have to use delete keyword to free up the heap memory. Java borrows heavely from C++ not the other way round.

  14. #14
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: How to declare global variable across multiple files?

    C++ has deterministic destructors which Java chose not to emulate. This gives an advantage to C++ when it comes to low memory usage and why Java, with its garbage collector can be a memory hog.
    It just a case of Swings and roundabouts.
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured