Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > static member variables across files...

Reply
Thread Tools

static member variables across files...

 
 
Rahul
Guest
Posts: n/a
 
      02-28-2008
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 ! ! !
 
Reply With Quote
 
 
 
 
Marcel Müller
Guest
Posts: n/a
 
      02-28-2008
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;
}
 
Reply With Quote
 
 
 
 
Rahul
Guest
Posts: n/a
 
      02-28-2008
On Feb 28, 1:24 pm, Marcel Müller <(E-Mail Removed)>
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?
 
Reply With Quote
 
programmer
Guest
Posts: n/a
 
      02-28-2008
>
> > #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.
 
Reply With Quote
 
nurxb01
Guest
Posts: n/a
 
      02-28-2008
On Feb 28, 4:19*pm, programmer <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Put variables into member variables or function variables? tjumail@gmail.com C++ 9 03-23-2008 04:03 PM
Can a static member function access non-static member? dolphin C++ 3 12-05-2007 12:39 PM
Can Derived class static member access protected member from base class? Siemel Naran C++ 4 01-12-2005 06:46 PM
How would I use qsort to sort a struct with a char* member and a long member - I want to sort in order of the long member Angus Comber C Programming 7 02-05-2004 06:41 PM
performance of static member function vs. instance member function 0to60 C++ 4 11-21-2003 05:25 PM



Advertisments