Scope of objects declared/defined in header file

Is the scope of all objects declared in a header file a file scope?

I know that objects in a cpp file declared outside of the main function are considered global variables, but I was wondering if that was the same for cpp files.
Last edited on
I thought it was the global scope.
I was asking wondering as well. Because integers can have the same signature between two header files and the compiler does not complain.
Last edited on
Are you saying you can declare two integers with the same names in different header files included by the same project?
I've always assumed the compiler doesn't care about files too much and sorts out the #include directives first.
#include is copy-paste.
I know that objects in a cpp file declared outside of the main function are considered global variables,
¿Have you done a project with several *.cpp? Then it comes the question if you want a global to be visible only in that source (static or unnamed namespace) or if all the project may access it (extern).
That gives the issues of 'redefinition of ...' at linking time (if you use nothing)

@Veltas
I've always assumed the compiler doesn't care about files too much and sorts out the #include directives first.


The compiler can not know that different translation units have the same definitions of an object. It is the linker that can find out that there are two or more definitions on the same object and it shall issue an error because it will not know which definition of the object to use.


@Flurite
Is the scope of all objects declared in a header file a file scope?


It is not necessary because an object can be declared inside a user defined namespace. So its scope is thsi namespace and can be enlarged by a using directive.
Last edited on
Topic archived. No new replies allowed.