Link to home
Start Free TrialLog in
Avatar of vikky999
vikky999

asked on

Scope of variable ?

I am trying to port a C program with 4 functions (including) main() into an MFC app.

The 4 functions communicate between themselves using GLOBAL VARIABLES.

In the MFC app it looks like....

void CMFCappDoc::OnRleEmbed()
{
func1();
func2();
func3();
}

However many of the variable names are identical to ones already used in the MFC. How do I declare the variables such that they are visible (ie.identical to global) inside the OnRleEmbed() function and accessible to func1(), func2() and func3(), but not outside OnRleEmbed() so that they do not interfere with the rest of the MFC app.

Thanx
Vivek
Avatar of frogger1999
frogger1999

there really isn't a way in C to my knowledge outside of putting all your globals inside a global struct and accesing them from inside the struct

e.g.

typedef struct globals{
    int global1;
    int global2;
}globals;

globals global;

however, since you are using MFC you must be using C++ and you can use namespaces to prevent name collisions

e.g.
#include <windows.h>
#include <iostream>

namespace mine{
int cout; //notice this global has the same name as the cout in iostream
}


int main()
{
    mine::cout = 5;
    std::cout<<mine::cout; //woo hoo used at the same time
    return 0;
}
Avatar of vikky999

ASKER

hmm..okay i think i get it.

But i have one more doubt in that case, I have created two files which i added to this project
Rle.h
Rle.cpp

Rle.h contains all the function declarations
Rle.cpp contains all the function definitions

In the MFCAppdoc.cpp file I include "Rle.h"

If ive defined some variables in Rle.h eg
int apple;
im guessing the variable apple is global in MFCAppdoc.cpp, am i right ?

but what if i define
int apple ;
in Rle.cpp ?? what is the scope of apple then ?

coz when i tried it in Rle.h i got error LNK 2005 , multiply defined symbols or something like that and it disspeared when i declared them in Rle.cpp. However now im not sure that the variable are in scope in MFCAppdoc.h.

I believe when i put a statement
#include "Rle.h"
the statement is simply replaced by the Rle.h file. so that would make sense and apple would obviously be global.
But I dont understand what happens if I declare it in Rle.cpp
yeah pretty much,  

#include is replaced by the contents of the file by the preprocessor.

if you put it in the cpp file (and its not externed) then MFCAppdoc.h will not know about "apple".

"externed" means that in MFCAppdoc.h you put a statement like so

extern int apple; //this tells the linker that you have a global apple defined elsewhere in your application

so if you need it in both files I would follow this example

so for an example

Rle.h looks like this then

//this #xxx stuff makes sure your .h is only included once in a file you should always do this regardless for your .h files of the example
#ifndef RLE_H_
#define RLE_H_

...
extern int apple;
...
#endif

in mfcappdoc.cpp do this

...
#include "Rle.h"
...


and in Rle.cpp do this

#include "Rle.h"

int apple;  //same rule applies as above

you can even include Rle.h in MFCAppDoc.h if necessary safely.

Anyway you should not put a variable definition in a .h file.  You should declare them (with extern) and then define them in a .cpp file.  That way you can avoid the multiply defined symbols stuff that you see.









It isn't clear to me whether you have access to the source code for func1(), etc. and if OnRleEmbed is the only top level function that needs them.

If that is the case, there's a simpler way.  Declare them as statics within OnRleEmbed, and pass them as parameters to func1(), etc.

The basic rule is to keep scope as small as possible.

Gary
ASKER CERTIFIED SOLUTION
Avatar of honey_hamster
honey_hamster

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks everybody. I kind of took some suggestions from all of you , i dont know how to distribute points.

THANKS !!!!!!

Vivek