Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does the compilation of a class work?

Tags:

c++

There are many references about compilation/linking process, but I am interested in a more concrete problem: compilation of a class.

The question arises because in general it is necessary to know things before you can use. For example: you can not call the function if it has not previously been declared.

In classes, it is not done in the same way. You can use a member before it appears. What the compiler does? Does the standard says something about a previous stage of compilation?

To be more specific, the following example shows how we can use members 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 further questions about inline functions, as may be defined after the point of call and the compiler can resolve without problems. I guess the answer is related.

UPDATE: I know the compilation/linkage has several steps. On the other hand, if the compiler accepts calling a function defined bellow is due that the compiler has analized the code in some sense. The question is ¿which kind of previous stage is done before? Moreover... in which part of the standard we find something related to use a member defined bellow?

To know how the compiler works is very interesting because it has to know details about the function bellow (at least, the header) that seems to correspond actually to compile. Even the data member has to be compiled because you have to relate its type in the context of the function above

It works like the code is reordered but it is not consistent with the example above because both function members call each other. It is like reordering data members and header of functions could be the code that is considered by the compiler.

like image 696
EFenix Avatar asked Jul 19 '16 10:07

EFenix


People also ask

How is a class compiled?

A Java class file is a compiled java file. It is compiled by the Java compiler into bytecode to be executed by the Java Virtual Machine.

How does the compilation process work?

The compilation is a process of converting the source code into object code. It is done with the help of the compiler. The compiler checks the source code for the syntactical or structural errors, and if the source code is error-free, then it generates the object code.

What happens during compilation of a program?

The compilation process in C is transforming a human-readable code into a machine-readable format. For C programming language, it happens before a program starts executing to check the syntax and semantics of the code.


1 Answers

The standard says

A class is considered a completely-defined object type (3.9) (or complete type) at the closing } of the class-specifier. Within the class member-specification, the class is regarded as complete within function bodies, default arguments, exception-specifications, and default member initializers (including such things in nested classes). Otherwise it is regarded as incomplete within its own class member-specification.

This in particular means that a member function body may refer to class members declared below it. The standard doesn't care how implementations achieve this. One possible method would be to postpone semantic analysis of member function bodies and other elements specified above until the closing brace of the class is seen.

like image 129
n. 1.8e9-where's-my-share m. Avatar answered Oct 16 '22 08:10

n. 1.8e9-where's-my-share m.