Click to See Complete Forum and Search --> : Static variable


GMV
November 24th, 2001, 03:08 AM
Hi all!
Please, say me
How can i use static struct in different .cpp file ? (In one workspace, of course).
Thx!

Igor Soukhov
November 24th, 2001, 04:42 AM
remove static from declaration.... put extern declaration to a shared header file and this structure will be accessible from all compilation units. in case if you can't remove static for some purposes - I see the one solution - create and "alias" variable (as the pointer to original structure and use this alias to access/modify the original structure:


//file1.cpp
#include "main.h"

extern str * ps;

void func1()
{
ps->i = 1;
};

//file1.h
void func1();


//file2.cpp
#include "main.h"

extern str * ps;

void func2()
{
ps->i = 2;
};

//file2.h
void func2();

//file main.h
struct str
{
int i;
};

//file main.cpp
#include "main.h"

#include <stdio.h>

static str s= {31};

str * ps = &s;//alias

#include "file1.h"
#include "file2.h"

int main()
{
printf("original s.i value = %d\n", s.i);
func1();
printf("after calling to func1() s.i value was changed to = %d\n", s.i);
func2();
printf("after calling to func2() s.i value was changed to = %d\n", s.i);


return 0;
};








Please - rate answer if it helped you
It gives me inspiration when I see myself in the top list =)

Best regards,

-----------
Igor Soukhov (Brainbench/Tekmetrics ID:50759)
igor@soukhov.com | ICQ:57404554 | http://soukhov.com

Russian Software Developer Network http://rsdn.ru