-5

I learned that the functions in class must be 'inline' from a book But, I can't understand why... That book wasn't enough I'm not native speaker in English, and so I'm very poor at searching in web described by Engilsh And my country's web sites is very closed to students like me So please answer me

I think If a function(not inline) were in a class, the function' s stackframe (some adresses, arguments and so on) would be created repeatedly whenever compiler refer to that function so this is why funtions in a class must be inline? Sorry for my poor English

Ray
  • 1
  • 1
    Member functions don't have to be inline. As Basile's answer explains, sometimes they are marked `inline` automatically, but it is not necessary to write the member function bodies inside the class in C++ (Java programmers may find this very strange). Also, [the C++ `inline` keyword does not actually have anything to do with the inlining optimization](http://stackoverflow.com/a/3212635/103167). – Ben Voigt Jan 04 '15 at 20:14

1 Answers1

1

In the definition of C++, if a member function is declared inside the class it is understood by the compiler as being inline

Notice that inline (including the implicit one for functions declared inside class) is just a hint to the compiler, which may or may not actually inline that function on some (or all or none) of its call sites. Inlining is always an optimization which the compiler is free to implement or not, and it often enables other optimizations too.

BTW, some compilers may inline functions not marked as inline, and that may be even done across compilation units with the so called link-time-optimization (for g++ compile and link with -flto -O2)

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547