Click here to Skip to main content
15,885,890 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
global.cpp:

C++
int global = 0;
int world = 0;

global.h:

C++
#ifndef GLOBAL_H
#define GLOBAL_H

extern int global;
extern int world;


#endif

main_1.cpp:

C++
#include "global.h"

int main1()
{ 
  set global = 1;  //read the value "1" from one device;

  get world;  // use world to do sth

}

main_2.cpp:

C++
#include "global.h"

int main2()
{
  set world = 222; //read the value "222" from another device

  get global;     //use global to do sth;
  }


-------------------------------------------------

I m a bit doubtful about what is the value of global in file main_2.cpp and the value of world in file main_1.cpp. What I expect is that the value of world or global can be stored and updated by different function.
Posted
Updated 19-Aug-11 17:24pm
v5
Comments
Emilio Garavaglia 19-Aug-11 5:44am    
what are set and get ? I see no declaration for them.
Philippe Mori 19-Aug-11 17:12pm    
Also are main1 and main2 called by main? And in which order?

1 solution

In your example, there is one and only one variable named "global". Same goes for "world". By your declarations, those are commonly shared by any and all accessors.

The value of "global" in Main_2.Cpp totally depends on the execution order of the functions main1() and main2(), which you didn't specify.

If we are to assume the functions are done in the order of main1() and then main2(), the value of global is 1 (as set in main1()) and not 0 (as set in the static initializer of globals.cpp).

Got it?
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 18-Aug-11 23:05pm    
Hope OP can get it. Your answer needs careful reading to understand. My 5.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900