1

Sorry if the questions are old or a bit stupid.

I know the basics of declaration and definition, but it seems in C++ there are lots of inconsistencies or "exceptions", which make it not harmonious, at least to me. Or, I have misunderstood something.

So, in header files, we declare some variables and define class types (also heard about "class declaration", no idea which is the more accurate); inside each class, we declare member variables and functions. Then in some .cc/.cpp file, we only define the member functions to implement the class. If "unfortunately" there is a static member variable/function, which would be considered special and fairly independent of the associated class, it has to be defined outside of the class, as the only kind of "freaks" among member variables.

When a .cpp file #includes a header file that defines a class type (note that the class type definition in that header file seems "incomplete" since its functions are defined somewhere else.), the .cpp file places the class type definition at the beginning, along with other possible declarations.

In the .cpp file, the static member variables of the #included class are already defined in the implementing .cpp along with the member functions, then what about the non-static "normal" member variables if an instance of the class is created? Suppose it's Class A;, then will the non-static member variables be defined if A a;? Of course an object a of type A is defined, but do its non-static member variables also get defined (defined in the sense of being allocated with memory)? If so, then two objects, say a1 and a2, of the same type A would be totally OK to be together because a1.mem_var_1 and a2.mem_var_1 have different definitions with respect to different class instance names? Or, they share the "same definition" but with different values and copies?

What about the member functions? They have been defined in the .cpp file for the class type, but when we create multiple instances, wouldn't they share the same definition in the .cpp but have different copies in the memory (I guess multiple copies of the same member function are necessary at least for different values of the local variables inside)? What about static functions?

OR, I'm misunderstanding something, because we programmers "define" something in a file is NOT EQUAL TO the compiler and linker "define" something in a file? What does definition really do and mean? We write in a file to "define" something, but the system can "define" differently, at least in the sense of secretly making multiple copies and stuff?

My head aches...

vincentvangaogh
  • 354
  • 3
  • 11
  • 2
    possible duplicate of [What is the difference between a definition and a declaration?](http://stackoverflow.com/questions/1410563/what-is-the-difference-between-a-definition-and-a-declaration) – NathanOliver Jul 07 '15 at 20:04
  • @ShafikYaghmour I would say it answers at least half of the question but I will go with it being too broad as this is multiple questions in one. I Just wanted to point the OP at some good information. maybe I should have said it was related. – NathanOliver Jul 07 '15 at 20:08
  • Yeah, it helps somewhat, but not all. – vincentvangaogh Jul 07 '15 at 20:22
  • @NathanOliver provides a useful link that answers about half of your question but as this question is written, it is simply too broad, it has too many questions and no specific examples. You need to come up with specific issue along with a [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve) that illustrates the issue if possible. – Shafik Yaghmour Jul 08 '15 at 12:10

1 Answers1

-1

The reasoning behind this is a historical one. When C was invented, memory and processor were small and expensive. If your project had 20 files, the compiler just couldn't load all in memory and resolve the symbols. Resolve means allocating enough and correct space for each function, structure and variable.

Now, imagine we have 2 cpp files, one defines the struct String, the other define the struct Rectangle. In order to have functions working with String and Rectangle in both files, the compiler needs to load both files and analyze them. Those times, this would be a very memory-intensive operation.

Therefore, some additional files, the headers were adopted. These headers just tell the compiler memory information about every type used by the cpp. Then the compiler is able to compile based just on the cpp file and all dependent headers into obj files.

After all cpp's were compiled, there comes linking, where all obj files are put together. Now it is critical that variables were defined the same way across all cpp's, otherwise some cpp's would work with a Rectangle of 8 bytes, and another cpp would work with a Rectangle of 12 bytes, and when put together -> bang!

AndreiM
  • 815
  • 9
  • 17