+ 2
Local to Global Variabel in C++ ?
How to declare a local variable in the scope but we can access it from outside the scope, in the sense that we set the local state of the variable to be a global variable
5 Answers
+ 3
Use dynamic memory allocation
+ 2
It's not possible, because variables are destroyed outside the scope.
The only way is to create a global variable:
int a = 1;
void f()
{
a = 100;
}
+ 2
Luk Mm okay, i think there is a rule or sign that make it happen
+ 2
You can use static variables as Mirielle[ Exams ] demonstrated.
But l want to warn you that using global variables is considered bad practise. Everything can get confusing really fast, especially when using it as Mirielle[ Exams ] showed. I don't know if that comes into the category of badly written code, but I would try to avoid it as much as possible. See this:
https://www.learncpp.com/cpp-tutorial/why-global-variables-are-evil/
+ 2
Mirielle[ Exams ]
XXX
Useful info, but i'll study that thx