Why would you use const?

void foo(const char *)

This 'foo' function requires constant char address, right? so what do I put in this parameter? is this correct?

1
2
const char x = 'a';
foo(&x);


and why would you use const? I still don't get it when people use const.
Well the function is taking a pointer to a char address so simply put when you call the function you would send it an address pointer. As for the const, you use const because you don't want the address to be changed. You want the address to remain "constant" throughout the scope of the function.
Last edited on
Hmm...I still don't understand. But did I do right with the &x ?

And why would you want your address to be const?
Yeah it looks right since you're pointing to the address of x.

And again ill try to explain why you use const,
If you didn't use const then what would happen is if somehow you managed to change the value of x in the foo() function then you would have a weird error that would piss you off till you found it. We don't want to be altering addresses or re-assigning them so we place const to prevent that from happening. So technically speaking you don't "have" to use const; but it is highly recommended and good practice to always add const to variables that are not going to be changed throughout the scope of a function.
Last edited on
 
void foo(const char *)
Here const means that foo is not allowed to modify what the pointer points to. So inside foo if you accidentally tried to modify the value the compiler would tell you.
1
2
3
4
5
void foo(const char * p)
{
	cout << *p << endl; // ok because *p is not modified
	*p = 5; // error: *p is not allowed to be modified
}

Also when you call foo you can be sure the function does not modify x.
1
2
3
4
char x = 'a';
foo(&x);
cout << x << endl; // You can be sure that 'a' will be outputted
                   // because foo is not allowed to modify x. 

Note that you can use foo even if x is const or not. If foo was declared without const it would only work if x was not const.
Last edited on
Why is foo not working if x is const?

1
2
3
4
5
6
7
8
9
void foo(char *input){
    cout << *input << endl;
}

int main(){
const char x = 'a';
    
    foo(&x);
}
Your foo does not promise to keep the x untouched. If the foo indeed changes the value of its parameter, then it would change the value of x, and x cannot be changed because it is const.
Other reason is that using "const" help compiler to optimize your program so it can run faster.
Well, there are many reasons you might use const...

Imagine you have a Game, so you have a window.
The window has a size, right?
When you declare it non-const, someone might think that he/she is allowed to change it's value.
So for example, you have a Window 640x480 and each frame you draw a grid
If someone sets the size to 320x480 the size of the window didn't change but the grid is only drawn in that area.
So, because that window size should be constant you declare it const.

Furthermore, in a function decleration, when not having the Keyword const before a pointer or reference you tell the compiler to give an error when a const variable is given as parameter.
Also, when you have a const-reference as parameter you may also use rvalues as parameters. (Most of the time when dealing with objects you want to pass them by reference, because it is faster)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Tree { int array[1000]; };

//...

void func(const Tree& tree) {} // save to call with every tree
void func2(Tree& tree) {} // this function may modify tree
void func2(Tree tree) {} // pass by value

int main(void)
{
// lvalues
    const Tree tree;
    func(tree); // valid
    func2(tree); // invalid
    func3(tree); // valid but needs to copy 1000 integers

// rvalues
    func(Tree()); // valid
    func2(Tree()); // invalid
    func3(Tree()); // valid but needs to copy 1000 integers
    
    return 0;
}
Last edited on
Topic archived. No new replies allowed.