I have a requirement where a (const) variable should be available throughout an entire cpp which consists of several classes. I have decided to use a namespace to solve the problem, but unsure about the following:
By default, an object or variable that is defined in the global namespace has static duration and external linkage. The static keyword can be used in the following situations.
Syntax and Use of the Static Variable in COne can define a static variable both- outside or inside the function. These are local to the block, and their default value is always zero. The static variables stay alive till the program gets executed in the end.
The static variables are stored in the data segment of the memory. The data segment is a part of the virtual address space of a program. All the static variables that do not have an explicit initialization or are initialized to zero are stored in the uninitialized data segment( also known as the BSS segment).
With functions: • When a function is called, a namespace for its variables is created. The function's parameters and any variables defined inside the function are placed into the function call's namespace. Variables in one namespace have NOTHING to do with variables of the same name in another namespace.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With