|
-
November 24th, 2001, 04:08 AM
#1
Static variable
Hi all!
Please, say me
How can i use static struct in different .cpp file ? (In one workspace, of course).
Thx!
-
November 24th, 2001, 05:42 AM
#2
Re: Static variable
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)
[email protected] | ICQ:57404554 | http://soukhov.com
Russian Software Developer Network http://rsdn.ru
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|