Nick
Nick

Reputation: 10499

Inline class method in the header file

Is there any differece between the following implementations?

class Foo
{
     int bar(int x) const
     { return x * 2; }
};


class Foo
{
     inline int bar(int x) const
     { return x * 2; }
};

Upvotes: 2

Views: 1149

Answers (3)

Amartel
Amartel

Reputation: 4286

7.1.2 Function specifiers

3 A function defined within a class definition is an inline function. The inline specifier shall not appear on a block scope function declaration. 94 (The inline keyword has no effect on the linkage of a function.) If the inline specifier is used in a friend declaration, that declaration shall be a definition or the function shall have previously been declared inline.

So, no. There is no difference.

Upvotes: 0

Vlad from Moscow
Vlad from Moscow

Reputation: 310930

According to the C++ Standard (9.3 Member functions)

2 A member function may be defined (8.4) in its class definition, in which case it is an inline member function (7.1.2), or it may be defined outside of its class definition if it has already been declared but not defined in its class definition.

And

3 An inline member function (whether static or non-static) may also be defined outside of its class definition provided either its declaration in the class definition or its definition outside of the class definition declares the function as inline.

Upvotes: 2

Adalee
Adalee

Reputation: 538

When the function is marked as inline, it tells the compiler to put the code of the function to the place where the function has been called from. You can have implicit and explicit inlines - member functions of classes are inlined implicitly, so you don't need to type it and in your case there is no difference.

Upvotes: 0

Related Questions