How does class compilation work?

There are many references to the compilation / binding process, but I am interested in a more specific problem: compiling a class.

The question is, because in general you need to know things before you can use them. For example: you cannot call a function if it has not been previously declared.

In classes, this is not the case. You can use the item until it appears. What does the compiler do? Does the standard say anything about the previous compilation step?

To be more specific, the following example shows how we can use the elements defined below.

#include <iostream> class EvenOdd { public: EvenOdd(): value(0) {} void assignEven(unsigned v) { if (v>0 && v%2==1) { std::cout << "Wrong call... is odd" << std::endl; assignOdd(v); } else { std::cout << "Right..." << v << " is Even" << std::endl; value= v; } } void assignOdd(unsigned v) { if (v>0 && v%2==0) { std::cout << "Wrong call... is even" << std::endl; assignEven(v); } else { std::cout << "Right..." << v << " is Odd" << std::endl; value= v; } } private: unsigned value; }; int main() { EvenOdd a; std::cout << "Do it right..." << std::endl; a.assignEven(2); std::cout << "doing it wrong..." << std::endl; a.assignEven(3); } 

We could also add additional questions about the built-in functions that can be defined after the dial-peer, and the compiler can solve without problems. I think the answer is related.

UPDATE: I know that compiling / linking has several steps. On the other hand, if the compiler accepts a call to the function defined below because the compiler has parsed the code in a way. The question is which of the previous steps has already been done? Also ... in what part of the standard do we find something related to using the member defined below?

Knowing how the compiler works is very interesting because it needs to know the details of the function below (at least the header), which seems to be true for compilation. Even the data item must be compiled because you need to bind its type in the context of the function above

It works as if the code is reordered, but it is not consistent with the above example, because both members of the function call each other. This is similar to reordering data items, and the function header may be code that is considered by the compiler.

+5
source share
2 answers

The standard says

A class is considered a fully defined type of object (3.9) (or a full type) when closing } the class specifier. Inside a class, a class is considered complete in function bodies, default arguments, exception specifications, and default element initializers (including such things in nested classes). Otherwise, it is considered incomplete within its class specification.

This, in particular, means that a member body can refer to members of the class declared below. The standard does not care how to achieve this. One possible way would be to postpone the semantic analysis of the member bodies and other elements mentioned above until the closing bracket of the class is noticed.

+7
source

Functions within classes can access and change (unless the function is a constant) data elements without declaring them, since data members are already declared in the class. In response to why the function does not require a declaration in the class.

0
source

All Articles