4

If a function (or simply a variable like int) defined in a header say "func.h", and being included in two object files a.o and b.o, then there will be duplication error when linking a.o and b.o. But this is not true for class, i.e. when a class definition in "class.h" and included both by a.o and b.o, there will be no problem.

What differences in compiling process for function/variables and class lead to this result? And what if two classes having the same name being linked?

chentingpc
  • 1,283
  • 3
  • 18
  • 24
  • @Drop I don't see how – YSC Dec 15 '15 at 22:32
  • @Drop I don't see it either. I am asking the differences in compiling process for processing class and functions/variables. – chentingpc Dec 15 '15 at 22:45
  • 2
    Here are better matches: [Why linker allows to have multiple class definitions with the same method defined?](http://stackoverflow.com/questions/10754350/why-linker-allows-to-have-multiple-class-definitions-with-the-same-method-define). [Same class name in different C++ files](http://stackoverflow.com/questions/10671956/same-class-name-in-different-c-files) – Ivan Aksamentov - Drop Dec 15 '15 at 22:45
  • @chentingpc I was trying to hint that variable (read object) is not the same as class. Including class produces no code. However it does not explain functions vs methods differences. So two links I've added above explain this as well. Anyway, duplicated classes are undefined behavior and there is not much to say about it. – Ivan Aksamentov - Drop Dec 15 '15 at 22:46
  • Look up the One Definition Rule. – Alan Stokes Dec 15 '15 at 22:49
  • And here is a link: [Definitions and ODR](http://en.cppreference.com/w/cpp/language/definition) – Ivan Aksamentov - Drop Dec 15 '15 at 22:50
  • @Drop Defining the same class in two different translation units is emphatically not undefined behaviour, as the links you posted explain. What do you mean by duplicated classes? – Alan Stokes Dec 15 '15 at 22:51
  • @AlanStokes I meant to write "duplicated class methods" ("duplicated" means redefined in different translation units) Precise wording was never my strong side ;( – Ivan Aksamentov - Drop Dec 15 '15 at 22:54
  • @Drop so by "Including class produces no code" you mean class definition produces no symbol (or object?) after compilation? I am not familiar with much details during compilation.. – chentingpc Dec 15 '15 at 22:59
  • I would expect it to give you an error for redeclaring the class. Are you sure that the "class.h" file does not have double-inclusion guards (i.e. "ifndef CLASS_H \n #define CLASS_H")? – Kirkova Dec 15 '15 at 23:01
  • @Kirkova the problem happens during linking, not compiling. #indef will be removed during preprocessing before compiling, so have no effect on linking I would say. – chentingpc Dec 15 '15 at 23:04
  • @Kirkova compilers are not required to diagnose redefinition between compilation units (how would you do this when using separate compilation?). – Ivan Aksamentov - Drop Dec 15 '15 at 23:04
  • @chentingpc I think that this link pretty much summarizes everything http://en.cppreference.com/w/cpp/language/definition – Ivan Aksamentov - Drop Dec 15 '15 at 23:12
  • @Drop I think I understand it from ODR perspective, which says it is fine for class as long as it is "consistent". But just trying to understand why class being treated differently from functions/variables? And I guess under the hood, during linking it will just pick one definition for class, and as long as ODR is obeyed, that's fine. – chentingpc Dec 15 '15 at 23:23

0 Answers0