Classes with the same name - is it restricted only within the same translation unit?
- by LeopardSkinPillBoxHat
Let's just I had the following code:
foo.h
class Foo
{
// ...
};
foo.cpp
#include "foo.h"
// Functions for class Foo defined here...
Let's say that Foo are built into a static library foo.lib.
Now let's say I have the following:
foo2.h
class Foo
{
// ...
};
foo2.cpp
#include "foo2.h"
// Functions for class Foo defined here...
This is built into a separate static library foo2.lib.
Now, if I re-link foo.lib and foo2.lib into an executable program foo.exe, should it be complaining that class Foo has been defined twice?
In my experiences, neither the compiler or the linker are complaining.
I wouldn't be expecting the compiler to complain, because they have been defined in separate translation units.
But why doesn't the linker complain?
How does the linker differentiate between the 2 versions of the Foo class? Does it work by decorating the symbols?