The only way is to pass this object to the function as an argument through a parameter that declared either as const reference or as a local variable of the function. For example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include <iostream>
int globalObject;
void f1( constint &x ) { x = 1; }
void f2( int x ) { x = 2; }
int main()
{
std::cout << "globalObject = " << globalObject << std::endl;
f1( globalObject );
f2( globalObject );
std::cout << "globalObject = " << globalObject << std::endl;
}
That's all seems pointless to me - the object is global - passing a reference to it, or a copy of it seems ...well ... pointless - it can be access directly anytime
from anywhere in the code.
The Point ist, that i either didnt declare the functions i have to define nor the main (That means the Interface is predefined). Furthermore i am not able to manipulate the main function.
I am writing a Plugin for another Programm, and that declares the functions it would like to have. (It works as a DLL)
Now i got no other choice as to define a global variable to exchange data from one function to another.
Thats the problem. And now i want to implement, that one function can read only, and another can manipulate.
The Question is: Is that even possible under these conditions.
It is possible if you will wrap the global variable in a class and the functions will be the class member functions one if which will have trailing const qualifier.:)