Re: Will two symbols with the same name clash?
* DeMarcus:
Hi,
Will two symbols with the same name but in different .cpp files clash?
Depends what you mean by "clash".
If the symbols have extern linkage then C++ generally does not support that.
It's called the "ODR", "One Definition Rule". But a linker may not necessarily
detect the clash -- e.g. 'int a[5]' one place and 'int* a' another place.
There are some execptions, where you are allowed multiple definitions of extern
linkage things.
The most important is "inline" routines.
A similar exception is there for static lifetime data in templates.
E.g.
file1.cpp
int varA;
file2.cpp
int varA;
Will varA clash during linking?
Depends on the linker.
But the code above is not well-defined C++.
Of course I can test it myself, but is
there a possibility that they are allowed to clash but the compiler
chooses one of them?
No, not with the example above.
You can do that via the exception for templates mentioned above.
E.g., in each compilation unit, ...
template< class Dummy >
struct Var_ { static int a; };
template< class Dummy >
int Var_<Dummy>::a = 0;
typedef Var_<void> Var;
.... then use 'Var::a'.
Sometimes I've seen compiler messages like: "multiple definitions of X,
ignoring the latter".
That sounds like you're in violation of C++ rules.
Cheers & hth.,
- Alf