Home:ALL Converter>Some questions about ODR, declaration, and definition

Some questions about ODR, declaration, and definition

Ask Time:2015-07-08T04:01:42         Author:vincentvangaogh

Json Formatter

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...

Author:vincentvangaogh,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/31278221/some-questions-about-odr-declaration-and-definition
yy