If I have two source files in a project that each define a class of the same name, what determines which version of the class is used?
For example:
// file1.cpp:
#include <iostream>
#include "file2.h"
struct A
{
A() : a(1) {}
int a;
};
int main()
{
// foo() <-- uncomment this line to draw in file2.cpp's use of class A
A a; // <-- Which version of class A is chosen by the linker?
std::cout << a.a << std::endl; // <-- Is "1" or "2" output?
}
…
//file2.h:
void foo();
…
// file2.cpp:
#include <iostream>
#include "file2.h"
struct A
{
A() : a(2) {}
int a;
};
void foo()
{
A a; // <-- Which version of class A is chosen by the linker?
std::cout << a.a << std::endl; // <-- Is "1" or "2" output?
}
I have been able to get different versions of A to be selected by the linker, with identical code – merely by changing the order in which I type the code (building along the way).
Granted, it’s poor programming practice to include different definitions of classes in the same namespace with the same name. However, are there defined rules that determine which class will be selected by the linker – and if so, what are they?
As a useful addendum to this question, I would like to know (in general) how the compiler / linker handles classes – does the compiler, when it builds each source file, incorporate the class name and compiled class definition within the object file, whereas the linker (in the scenario of a name clash) throws away one set of compiled class function/member definitions?
The issue of a name clash is not arcane – I now realize that it happens EVERY TIME a header-only template file is #included by two or more source files (and subsequently the same template classes are instantiated, and the same member functions called, in these multiple source files), as is a common scenario with the STL. Each source file must have a separately-compiled version of the same instantiated template class functions, so the linker MUST be selecting among different such compiled versions of these functions at linkage time), I would think.
— ADDENDUM with related question about Java —
I note that various answers have indicated the One Definition Rule (http://en.wikipedia.org/wiki/One_definition_rule) for C++. As an interesting aside, am I correct that Java has NO SUCH rule – so that multiple, different definitions ARE allowed in Java by the Java specifications?
If a C++ program provides two definitions of the same class (i.e., within the same namespace and named identical), the program violates the rules of the standard and you’ll get undefined behavior. What exactly does happen somewhat depends on the compiler and linker: sometimes you get a linker error but this isn’t required.
The obvious fix is not to have conflicting class names. The easiest approach to obtain unique class names is to define locally used types within an unnamed namespace:
These two classes won’t conflict.