Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 212
  • Last Modified:

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()

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.

1 Solution
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


typedef struct globals{
    int global1;
    int global2;

globals global;

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

#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;
vikky999Author Commented:
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 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;

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.


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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.

If the variables have to be visible to func1(), func2(), and func3(), then you can put func1(), func2(), func3() all in the same .c source file, and declare the variables as static at the top of the .c source file.  That way, all code within the .c file can see the variables, but the variables remain hidden and separate from all software outside your .c file.
vikky999Author Commented:
Thanks everybody. I kind of took some suggestions from all of you , i dont know how to distribute points.

THANKS !!!!!!


Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now