Engineer999
Engineer999

Reputation: 3945

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

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 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. Is this true?

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

Upvotes: 1

Views: 2304

Answers (4)

Ethouris
Ethouris

Reputation: 1901

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).

Upvotes: 1

Mike Seymour
Mike Seymour

Reputation: 254431

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.

Upvotes: 2

Mykhaylo Kopytonenko
Mykhaylo Kopytonenko

Reputation: 953

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.

Upvotes: 0

yUdoDis
yUdoDis

Reputation: 1098

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.

Upvotes: 0

Related Questions