Anonymous namespace is kind of namespace with no specified name. Such kind of namespace is visible only within file where it is defined within. I allows define variables and functions which could be visible as global, but within one file only. Thanks to that such files are not global within whole program. It also allow avoids making global static variable.
Let's see an example of defining and usage of anonymous namespace. Consider following main.cpp file:
In the above example we have anonymous namespace containing one int typed variable. This variable can be used as global variable withing this main.cpp file but will not be visible inside any other files. That is simple example of usage anonymous variables. If you would like to compile it and play with it (changing or adding some parts of code to check what happends than) you can download it from here: https://github.com/xmementoit/CppAdventureExamples/tree/master/advancedCpp/anonymousNamespace
In the above example we have anonymous namespace containing one int typed variable. This variable can be used as global variable withing this main.cpp file but will not be visible inside any other files. That is simple example of usage anonymous variables. If you would like to compile it and play with it (changing or adding some parts of code to check what happends than) you can download it from here: https://github.com/xmementoit/CppAdventureExamples/tree/master/advancedCpp/anonymousNamespace
Comments
Post a Comment