static member variables across files...

Discussion in 'C++' started by Rahul, Feb 28, 2008.

  1. Rahul

    Rahul Guest

    Hi Everyone,

    I have a class having a static member variable in a header file.

    File.h

    class india
    {
    public:
    static int a;
    };

    File1.cpp

    #include "file.h"
    int india::a = 5;

    Now i want to access the same static member variable in another file
    File2.cpp, what is the best possible way to do so?

    Thanks in advance ! ! !
    Rahul, Feb 28, 2008
    #1
    1. Advertising

  2. Rahul schrieb:
    > #include "file.h"
    > int india::a = 5;
    >
    > Now i want to access the same static member variable in another file
    > File2.cpp, what is the best possible way to do so?



    #include "file.h"

    void foo()
    { ++india::a;
    }
    Marcel Müller, Feb 28, 2008
    #2
    1. Advertising

  3. Rahul

    Rahul Guest

    On Feb 28, 1:24 pm, Marcel Müller <>
    wrote:
    > Rahul schrieb:
    >
    > > #include "file.h"
    > > int india::a = 5;

    >
    > > Now i want to access the same static member variable in another file
    > > File2.cpp, what is the best possible way to do so?

    >
    > #include "file.h"
    >
    > void foo()
    > { ++india::a;
    >
    > }


    Would the static variable india::a be initialized for sure by the
    time, linker is working on file2.o?
    Rahul, Feb 28, 2008
    #3
  4. Rahul

    programmer Guest

    >
    > > #include "file.h"

    >
    > > void foo()
    > > { ++india::a;

    >
    > > }

    >
    > Would the static variable india::a be initialized for sure by the
    > time, linker is working on file2.o?


    I think it works, you can access the india::a in the file2.cpp when
    the file1.cpp is in your project or makefile.If you do not include the
    file1.cpp in your makefile, the class will not be implemented, the
    linker will not work properly. So you must include the implemented
    file in your makefile and the india::a will be initialized once.
    programmer, Feb 28, 2008
    #4
  5. Rahul

    nurxb01 Guest

    On Feb 28, 4:19 pm, programmer <> wrote:
    > > > #include "file.h"

    >
    > > > void foo()
    > > > { ++india::a;

    >
    > > > }

    >
    > > Would the static variable india::a be initialized for sure by the
    > > time, linker is working on file2.o?

    >
    > I think it works, you can access the india::a in the file2.cpp when
    > the file1.cpp is in your project or makefile.If you do not include the
    > file1.cpp in your makefile, the class will not be implemented, the
    > linker will not work properly. So you must include the implemented
    > file in your makefile and the india::a will be initialized once.


    Static members are only gauranteed to be initiallized before call to
    main() happen.
    But if there has been a call made to function foo() while constructing
    a global static or global object, there are good chances that the
    function call will give wrong results.
    nurxb01, Feb 28, 2008
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. 0to60
    Replies:
    4
    Views:
    503
    jeffc
    Nov 21, 2003
  2. dolphin
    Replies:
    3
    Views:
    1,326
    Pete Becker
    Dec 5, 2007
  3. Replies:
    9
    Views:
    928
  4. Replies:
    5
    Views:
    294
    dizzy
    May 7, 2008
  5. Robert Crandal
    Replies:
    15
    Views:
    4,414
    Robert Crandal
    Mar 24, 2010
Loading...

Share This Page