-5

I would like to send a value of a Boolean from file B.cc to A.cc, where A.cc is executed before B.cc for the next cycle of operation. I have the boolean value as an extern volatile and as a global variable. How do I save the value till the next cycle of operations.

A.cc

#include "linker.h"
#include "a.h"
bool right;
void a()
{
     std::cout << right << std::endl;
}

B.cc

#include "linker.h"
#include "B.h"
bool right;
void B()
{
     if(a%2 == 0)
     {
        right = false;
     }
     else right = true;
}

B.h

#ifdef _B_
#define _B_
int a
#endif

Linker.h

#ifdef _linker_
#define _linker_
extern volatile right;
#endif
Charan Karthikeyan
  • 57
  • 1
  • 2
  • 12

1 Answers1

1

Depending on the environment and requirements, the best method to send values "between source files" is by passing parameters to the necessary functions or methods.

Research "data hiding" and "encapsulation". To keep you sanity, you'll want to minimize or eliminate global variables. (Think about signalling, ownership, multithreading access, etc.)

Another method is to declare the variable with static file scope and write getter and setter functions.

Research the internet for "C++ extern variable".

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
  • I cannot pass the value through a function since the functions cannot be tampered with.I also tried setting the variable to static, but that too did not solve the problem – Charan Karthikeyan Mar 27 '18 at 20:03