Can I make global variables const in some functions?

Suppose I have some variables that are changed only in a couple of functions, but which need to be accessed in a bunch of other functions.

One way is to pass these variables to all functions, as const to functions which should not change them.
This is a bit inelegant because it introduces clutter whenever I call functions.
Is there another way where I can state in a function body something like "for the scope of this function, treat these variables as const"?
Is there another way where I can state in a function body something like "for the scope of this function, treat these variables as const"?

No. Anything you could with global variables is tantamount to a comment.


Suppose I have some variables that are changed only in a couple of functions, but which need to be accessed in a bunch of other functions.

Perhaps you should consider the use of encapsulation.
Last edited on
if you pass a variable to a function, it simply gets a copy of it. it won't change the actual variable.
if you want to change the variable, you have to use pointers
If you do not need to have a guarantee that those functions will not be ever able to change value of global, and simply want to protect yourself from accidental error, you can make a const reference to this global:
1
2
3
4
5
6
7
8
9
int foo = 5; // global

//..
void bar()
{
    const int& baz = foo;
    std::cout << baz; //Outputs 5
    baz = 5; // Error
}
However you should really consider changing your aprogram and encapsulate them in some way. Remember, names of best globals start with //
Interesting solution MiiNiPaa.
What did you mean by "encapsulation"?

BTW, would this be legal:
1
2
3
4
5
6
7
8
9
10
int foo = 5; // global

//..
void bar()
{
    const int& baz = foo;
    const bool foo = false;
    std::cout << baz; //Outputs 5
    baz = 5; // Error
}


The defintion of foo in the function block is to hide the original definition.
Last edited on
The defintion of foo in the function block is to hide the original definition.
It is useless. If you do not want to use it, then do not use it. If somebody will want to use it, it is still possible.

Most language safety features protects against misuse, not malicious user. It is possible to circumvent them anyway.

you can use static variable/function in class. keep the variable private.
all functions can see its value. but only friend functions can modify it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
using namespace std;

class Something
{
private:
    static int nValue;  //you can use it without creating class object !
public:
    static int GetValue() { return nValue; }
    friend void func();
};
 
int Something::nValue = 1; // initializer

void func(){
	Something::nValue = 8; // modify
}

 
int main()
{
    cout << Something::GetValue() << endl;    
    func(); 
    //Something::nValue = 9; // error, you cant do here.
    cout << Something::GetValue() << endl;
}


output:
1
8


static member function: http://www.learncpp.com/cpp-tutorial/812-static-member-functions/
Last edited on
Topic archived. No new replies allowed.