I understand that you can use the static
keyword for functions and variables to prevent them from being visible from other files. Can I use it for classes/structs? For example, inside a function, I use a struct that only that function uses, but I don't want to be able to use it from outside that file and I don't return that struct.
Asked
Active
Viewed 53 times
1

W. Albuquerque
- 315
- 2
- 12
-
You don't need to? Unless you are talking about having a variable of class type at file scope? – UnholySheep Jul 15 '20 at 13:57
-
@UnholySheep C++ has ODR – user253751 Jul 15 '20 at 13:57
-
`static` is not a visibility modifier. It controls *storage duration* and/or *linkage* (depending on context). – 0x5453 Jul 15 '20 at 13:58
-
@user253751 it does. – W. Albuquerque Jul 15 '20 at 14:00
1 Answers
0
Can I use the static keyword for classes and structs in C++?
No. However, you can define classes in an anonymous namespace which will prevent it from being named in other translation units.

eerorika
- 232,697
- 12
- 197
- 326