O'Reilly logo

SAMS Teach Yourself C++ in 10 Minutes SECOND EDITION by Jesse Liberty

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Global Variables

It is possible to have variables that are not in any specific function. Such a variable is called a global variable and can be declared at the top of the module's .cpp file.

Because a global variable is not “in” a particular function, it is visible to all functions. This means that any function can get or change its value.

This increases the dependence (coupling) between any functions that use the global variable, which makes maintenance more difficult. For this reason, you should avoid the use of global variables, using arguments and return values instead.

One other issue with global variables arises when local and global variables have the same name. In these instances, the local variable takes priority over the global variable, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required