Inline keyword in member function definition

c++

Solution

`inline` has some pre-historic use, but nowadays it's best to remember it as saying: "this definition is going to be defined multiple times, and that's okay."

That is, normally the one-definition rule prohibits multiple definitions of a function. This:

// foo.hpp
void foo() { /* body */ }

// a.cpp
#include "foo.hpp"

// b.cpp
#include "foo.hpp"

results in an error, as `foo` is defined in two translation units. You can declare things as often as you want. This:

// foo.hpp
void foo();

// foo.cpp
void foo()
{
    /* body */
}

// a.cpp
#include "foo.hpp"

// b.cpp
#include "foo.hpp"

is fine, as `foo` is defined once, and declared multiple times. What `inline` does is allow this:

// foo.hpp
inline void foo() { /* body */ }

// a.cpp
#include "foo.hpp"

// b.cpp
#include "foo.hpp"

to work. It says "if you see `foo` more than once, just assume they are the same and be okay with it".

Problem

Why inline keyword should used in the definition of member function. and Not in declaration?

Original source