Howdy, Stranger!

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

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

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.