0

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.

abraham_hilbert
  • 2,221
  • 1
  • 13
  • 30

2 Answers2

1

Typically the linker is mostly aware of functions and variables (non member variables).

The information the linker holds in regards to classes is usually related to the implementation of inheritance. Member functions are translated to regular function, with additional implicit this parameter. Access to members is usually just an offset into this pointer.

If you however add inheritance to your classes, or use typeid or type_info you might start to encounter many kinds of weird behaviour.

OriBS
  • 722
  • 5
  • 9
1

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.

Yakk - Adam Nevraumont
  • 262,606
  • 27
  • 330
  • 524