-
The inlines in C + +
Meaning
The keyword inline is used in C + + and applies to a function. It tells the compiler that each function call inline will be replaced by the body of this function. To generate an executable reasonable size so it is in practice used for functions "short" in terms of number of instructions.
The keyword inline has the advantage of speed up a program if it is regularly call the function inline. It helps significantly condense the code, especially for accessors of a class. A accessor class is typically a function of a line
Example
Code:
# include <iostream>
inline void my_function () (
std:: court << "I like tapirs" <<std:: endl;
)
(class ma_structure_t
protected:
int x;
public:
ma_structure_t (int x0): x (X0) ()
inline int get_x () const (
return x;
)
);
int main () (
my_function ();
ma_structure_t s (7);
std:: court <<s.get_x () <<std:: endl;
return 0;
)
Inline and headers
The inline can declare and implement functions directly in a header (. Hpp) without risk of multi-definition. Indeed, the symbol of the civil inline never explicitly at the time of linkage since the calls for this function have all been replaced. Remember that if a function is not inline, is implemented in a header, and that the header is included in several places, the compiler will return an error of multiple definition.
Remember that apart functions templates, only inline functions can be implemented in the header. A function template may well be inline.
If you want to encode a function inline declared in a header (. Hpp), but implemented in a source file (. Cpp) only the prototype header must include the keyword inline.
Example
fichier.hpp
fichier.cpp:
Code:
# include <iostream>
# include "fichier.hpp"
void f () (
std:: court << "plop!" <<Std:: endl;
)
Page generated in 1,717,393,493.97291 seconds with 10 queries