Skip to main content
added 253 characters in body
Source Link
Giorgio
  • 19.7k
  • 16
  • 87
  • 137

Java works in a similar way: when then compiler imports a class A into class B, it looks at the class file for A and does not need the file A.java. So all definitions and initializations for class A can be put in one source file.

Going back to C++, the reason why in C++ you have to define static data members in a separate file is more related to the C++ compilation model than to limitations imposed by the linker or other tools used by the compiler. In C++, importing some symbols means to build their declaration as part of the current compilation unit. This is very important, among other things, because of the way in which templates are compiled. But this implies that you cannot / should not define any global symbols (functions, variables, methods, static data members) in an included file, otherwise these symbols could be multiply-defined in the compiled object files.

So the reason why in C++ you have to define static data members in a separate file is more related to the C++ compilation model than to limitations imposed by the linker or other tools used by the compiler. In C++, importing some symbols means to build their declaration as part of the current compilation unit. This is very important, among other things, because of the way in which templates are compiled. But this implies that you cannot / should not define any global symbols (functions, variables, methods, static data members) in an included file, otherwise these symbols could be multiply-defined in the compiled object files.

Java works in a similar way: when then compiler imports a class A into class B, it looks at the class file for A and does not need the file A.java. So all definitions and initializations for class A can be put in one source file.

Going back to C++, the reason why in C++ you have to define static data members in a separate file is more related to the C++ compilation model than to limitations imposed by the linker or other tools used by the compiler. In C++, importing some symbols means to build their declaration as part of the current compilation unit. This is very important, among other things, because of the way in which templates are compiled. But this implies that you cannot / should not define any global symbols (functions, variables, methods, static data members) in an included file, otherwise these symbols could be multiply-defined in the compiled object files.

deleted 4 characters in body
Source Link
Giorgio
  • 19.7k
  • 16
  • 87
  • 137

So IMO the reason why in C++ you have to define static data members in a separate file is more related to the C++ compilation model than to limitations imposed by the linker or other tools used by the compiler. In C++, importing some symbols means to build their declaration as part of the current compilation unit. This is very important, among other things, because of the way in which templates are compiled. But this implies that you cannot / should not define any global symbols (functions, variables, methods, static data members) in an included file, otherwise these symbols could be multiply-defined in the compiled object files.

So IMO the reason why in C++ you have to define static data members in a separate file is more related to the C++ compilation model than to limitations imposed by the linker or other tools used by the compiler. In C++, importing some symbols means to build their declaration as part of the current compilation unit. This is very important, among other things, because of the way in which templates are compiled. But this implies that you cannot / should not define any global symbols (functions, variables, methods, static data members) in an included file, otherwise these symbols could be multiply-defined in the compiled object files.

So the reason why in C++ you have to define static data members in a separate file is more related to the C++ compilation model than to limitations imposed by the linker or other tools used by the compiler. In C++, importing some symbols means to build their declaration as part of the current compilation unit. This is very important, among other things, because of the way in which templates are compiled. But this implies that you cannot / should not define any global symbols (functions, variables, methods, static data members) in an included file, otherwise these symbols could be multiply-defined in the compiled object files.

added 4 characters in body
Source Link
Giorgio
  • 19.7k
  • 16
  • 87
  • 137

On the other hand, it is possible to have a compilation model in which the compiler knows very well if it is importing the declarationimporting the declaration of a global symbol that is defined in another moduledefined in another module, or if it is compiling the definitioncompiling the definition of a global symbol provided by the current module. Only in the current module and therefore itlatter case the compiler must put this symbol (e.g. a variable) in the current object object file.

On the other hand, it is possible to have a compilation model in which the compiler knows very well if it is importing the declaration of a global symbol that is defined in another module, or if it is compiling the definition of a global symbol in the current module and therefore it must put this symbol (e.g. a variable) in the current object file.

On the other hand, it is possible to have a compilation model in which the compiler knows very well if it is importing the declaration of a global symbol that is defined in another module, or if it is compiling the definition of a global symbol provided by the current module. Only in the latter case the compiler must put this symbol (e.g. a variable) in the current object file.

Improved formulation.
Source Link
Giorgio
  • 19.7k
  • 16
  • 87
  • 137
Loading
Source Link
Giorgio
  • 19.7k
  • 16
  • 87
  • 137
Loading