extern variables

This is a discussion on extern variables within the C++ Programming forums, part of the General Programming Boards category; Hello, Suppose I have a class, MyClass, which contains variables (int x, y, z) which I want to access throughout ...

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    4

    extern variables

    Hello,

    Suppose I have a class, MyClass, which contains variables (int x, y, z) which I want to access throughout my program. I want to be able to use the class in a static sense. I want to be able to access the variables using something like "MyClass::x", without actually having to create a MyClass object. So, I need to initialize these variables somewhere, which I gather requires the use of "extern".

    Here is what I have so far:

    Code:
    // myclass.cpp
    class MyClass
    {
        int x;
        int y;
        int z;
    };
    
    // main.cpp
    int main()
    {
        extern MyClass::x = 4;
        extern MyClass::y = 5;
        extern MyClass::z = 8;
    
        return 0;
    }
    But this is giving me a load of error messages, so the approach is obviously wrong. How should I go about it?

    Thanks

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,884
    Quote Originally Posted by karnavor
    I want to be able to access the variables using something like "MyClass::x", without actually having to create a MyClass object.
    You can define the class as:
    Code:
    class MyClass
    {
    public:
        static int x;
        static int y;
        static int z;
    };
    Then in exactly one source file, write:
    Code:
    int MyClass::x = 4;
    int MyClass::y = 5;
    int MyClass::z = 8;
    This will be effectively using the class purely as a namespace.

    Are you really sure that you need such global variables in the first place? Or do you want global constants instead?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Aug 2009
    Posts
    4
    Well yes, actually I want them to be constants as they will not change. Can I do it in the same way, but using "const static int x" instead of just "static int x"?

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Wouldn't they be better as defines then?:
    Code:
    #define MYX 4
    #define MYY 5
    #define MYZ 6
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,884
    In that case, since they are all integral class constants, this will suffice:
    Code:
    class MyClass
    {
    public:
        static const int x = 4;
        static const int y = 5;
        static const int z = 8;
    };
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Aug 2009
    Posts
    4
    It seems to work well when I initialize the values in a source file:
    Code:
    int MyClass::x = 4;
    int MyClass::y = 5;
    int MyClass::z = 8;
    But I thought that it was bad practice to have global operations such as this. So, I tried to create an initializer function which would perform this step inside a class:
    Code:
    void InitializeVariables()
    {
    int MyClass::x = 4;
    int MyClass::y = 5;
    int MyClass::z = 8;
    };
    But then I get the following error: "definition or redeclaration illegal in current scope". Does this mean that these values always have to be defined in global scope?

  7. #7
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by MK27 View Post
    Wouldn't they be better as defines then?:
    Code:
    #define MYX 4
    #define MYY 5
    #define MYZ 6
    No, const is the way to go. I only use #define if I have no other choice.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,884
    Quote Originally Posted by karnavor
    But I thought that it was bad practice to have global operations such as this.
    The bad practice is to have global variables in the first place. But since they are global constants, it is quite okay, especially since you are just initialising them to literal values (i.e., there can be no static initialisation order problem where you initialise them with other namespace-level objects, from another translation unit, that might not have been properly initialised at that point).

    Quote Originally Posted by karnavor
    So, I tried to create an initializer function which would perform this step inside a class:
    You cannot initialise them in that way; you can only assign to them, but obviously you also cannot do that since you want them to be const.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,756
    Another option is a namespace...
    Code:
    namespace MyNameSpace
    {
        const int x = 0;
        const int y = 1;
        const int z = 2;
        // etc
    }
    It looks better than abusing a class for global constants that usually has little do with the class itself.
    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. extern const?Please help
    By huwan in forum C++ Programming
    Replies: 10
    Last Post: 08-12-2008, 04:53 AM
  2. extern classes
    By Swordsman in forum C++ Programming
    Replies: 1
    Last Post: 05-07-2008, 02:07 AM
  3. need help with extern pointers.
    By broli86 in forum C Programming
    Replies: 17
    Last Post: 04-11-2008, 09:16 AM
  4. Declaring an variable number of variables
    By Decrypt in forum C++ Programming
    Replies: 8
    Last Post: 02-27-2005, 03:46 PM
  5. Global variables keep resetting
    By earnshaw in forum C Programming
    Replies: 6
    Last Post: 01-19-2005, 09:35 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21