How to declare an object const for one function and not for an other?

Helle there,

i got an global object and i got global functions. Both predefined.
How can i set the object const for one function and not for the other?

Thanks for the Answers.
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( const int &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;
}
Last edited on
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.
> i got an global object and i got global functions. Both predefined.
> How can i set the object const for one function and not for the other?

You can't. http://en.wikipedia.org/wiki/One_Definition_Rule
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.:)
Yeah, it isnt optimal, but if its the only possibility i am going to do it that way. But nowadays i have some other trouble to fix first :)
Topic archived. No new replies allowed.