Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

how to initialize variables in one file through all other files

deep725deep725 Posts: 3Member
HELLO FRENZ
would u help me by telling how can i initialize and access variables declared in one file through all other files in VC++. If needed then plz explain me by giving code.please helpp me as it is very urgent.

thank-you

Comments

  • AsmGuru62AsmGuru62 Posts: 6,519Member
    : HELLO FRENZ
    : would u help me by telling how can i initialize and access variables declared in one file through all other files in VC++. If needed then plz explain me by giving code.please helpp me as it is very urgent.
    :
    : thank-you
    :
    [blue]Your explanation of a problem is not quite clear, but if you are talking about the global variables then put them in one file:
    [code]
    // GLOBALS.CPP
    int glb_iValue;
    char glb_strPath [256];
    // ^^^ whatever more here...

    // Put a function to init variables:
    void InitGlobals ()
    {
    // Set them up here whatever way you need...
    // load them from file... etc.
    glb_iValue = 4;
    strcpy (glb_strPath, "whatever...");
    }

    // Put a function to release your variables:
    void DoneWithGlobals ()
    {
    // Put code here to release your global objects...
    }
    [/code]
    Then make a header file to use in all other files:
    [code]
    // GLOBALS.H
    extern int glb_iValue;
    extern char glb_strPath [256];
    // ^^^ exact replica of vars, just put the 'extern' before them...

    // Prototypes of functions inside GLOBALS.CPP
    void InitGlobals ();
    void DoneWithGlobals ();
    [/code]
    Now, include GLOBALS.H into every other CPP file in your project and call [b]InitGlobals()[/b] when you begin your program and call [b]DoneWithGlobals()[/b] when you end it:
    [code]
    #include "globals.h"

    int main ()
    {
    InitGlobals ();

    // Your program goes here...

    DoneWithGlobals ();
    return 0;
    }
    [/code]
    That is all!
    [/blue]
Sign In or Register to comment.