The runtime constant declaration

How do we have runtime constant declaration/definition ?


Apologize I feel neglectful how it is definitively as oppose to compile time constant const
Last edited on
You could think of const as meaning "read-only".

1
2
3
4
5
6
void foo(int x)
{
	const int y = 2 * x;
	
	...
}

In the above example y is obviously not a compile-time constant because the value of y is not known when the program is compiled. It depends on what value is passed to the function.

The code that follows (imagine there is more code where the dots are) can read the value of y but it cannot modify it.

1
2
std::cout << y << "\n"; // OK
y = 5; // Error 
Last edited on
Topic archived. No new replies allowed.