CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Static variable

  1. #1
    Join Date
    Nov 2000
    Location
    USA
    Posts
    179

    Static variable

    Hi all!
    Please, say me
    How can i use static struct in different .cpp file ? (In one workspace, of course).
    Thx!


  2. #2
    Join Date
    Feb 2001
    Location
    Sydney, Australia
    Posts
    1,909

    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
    Best regards,
    Igor Sukhov

    www.sukhov.net

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured