Classes with the same name - is it restricted only within the same translation unit?

Posted by LeopardSkinPillBoxHat on Stack Overflow See other posts from Stack Overflow or by LeopardSkinPillBoxHat
Published on 2010-05-12T05:46:42Z Indexed on 2010/05/12 5:54 UTC
Read the original article Hit count: 252

Filed under:
|
|
|

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?

© Stack Overflow or respective owner

Related posts about c++

Related posts about translation-unit