Search code examples
c++templateslinkercompilationunit

How does the compiler and linker handle classes and functions


Can someone please help me to understand, why I can have the same class in two different compilation units without getting linker errors but not the same function in different compilation units?

Many thanks in advance.


Solution

  • The same class in two compolation units is likely to lead to an ill-formed program due to one definition errors from its methods (including special members, like default ctor or operator=) having the same name.

    My special member functions are defined implicitly inline, so we get ill-formed programs with no diagnostic required quite easy.

    I have corrupted memory due to this; two matrix classes with the same name and different memory layout.