Defining a member function inside the class instead of outside in C++?

前端 未结 4 686
北海茫月
北海茫月 2021-01-24 13:23

By writing the definition of a member function inside the class in the header file as follows, I was told that compiler treats this as an inline function - hence , anywhere it e

相关标签:
4条回答
  • 2021-01-24 14:00

    I was told that compiler treats this as an inline function

    Correct. That means that it's allowed to be defined in more than one translation unit, which is necessary if you want to define it in a header and include that header more than once in your program.

    Hence , anywhere it encounters this function being called in the program , it enters the complete body of the function every time instead of jumping to the one location where the function resides in memory

    No, that's not what inline means in the context of a function definition. That's the optimisation of function call inlining, which the compiler can apply to any call if it thinks it will improve the code.

    The two concepts are loosely related: compilers which process a single translation unit at a time will only be able to apply the optimisation to functions defined in the current unit, so they'll need to have multiple definitions to be able to optimise them in more than one unit.

    0 讨论(0)
  • 2021-01-24 14:01

    You are right that the function with definition in the header file is implicitly "inline". But the inline keyword is just a hint to the compiler. Depending on its implementation, on the function contents and on the optimization flags, the compiler may decide to inline a function or not.

    0 讨论(0)
  • 2021-01-24 14:08

    If you define a function within a class definition, it is by default inline ( without needing to specify 'inline')

    The compiler replaces all instances of an inline function call with the actual code.

    Note: the compiler may ignore the 'inline' qualifier of a function if it is more than one line. You will also need to recompile all componnents (the ones which utilize that function) so that compiler can replace the code block with the new code.

    0 讨论(0)
  • 2021-01-24 14:24

    If your question is whether this is equivalent to:

    class myClass
    {
    public:
       void Fun1();
    };
    
    inline void myClass::Fun1()
    {
         //implemented here
    }
    

    the answer is: YES.

    If your question is whether the function will be physically expanded inline, the answer is: UNDEFINED. Depends on compiler's optimization flags and rules how it works.

    The meaning of "inlining" in C++ is: if the compiler emits this function as out-of-line (either by not expanding at all or as alternative), it undergoes weak (vague) linkage (two such functions in two different object files linked together results in taking the first one as a good deal).

    That's in contrast to normal external function, which undergo strong linkage (two such functions in two different object files linked together results in linker error).

    0 讨论(0)
提交回复
热议问题