inline function in a class

K

kceiw

How to make a member function inline while the declaration and
implementation are separated?
 
S

Squeamizh

kceiw said:
How to make a member function inline while the declaration and
implementation are separated?

If you want to keep the declaration and body separate, then the easiest
way to do it is by puttin the function body in the same header file as
the class definition, but outside the actual class.

E.G.:

--- BEGIN EXAMPLE.H ----
class Example {
public:
Example();
inline void helloWorld() const;
};

void Example::helloWorld() const {
std::cout << "hello world";
}
--- END EXAMPLE.H ---
 
T

Thomas Tutone

Squeamizh said:
If you want to keep the declaration and body separate, then the easiest
way to do it is by puttin the function body in the same header file as
the class definition, but outside the actual class.

E.G.:

--- BEGIN EXAMPLE.H ----
class Example {
public:
Example();
inline void helloWorld() const;
};

void Example::helloWorld() const {
std::cout << "hello world";
}
--- END EXAMPLE.H ---

Your example wouldn't result in the member function being declared
inline, as the OP specified. I think perhaps you meant something like:

class Example {
public:
void memFunc() const;
};

inline void Example::memFunc() const
{
doSomething();
}

Best regards,

Tom
 
T

tmartsum

There is no way to completly force inline ....
(what if your function is recursive ? =) )

inline gives a hint (gcc will (normally?) respect this when given the
right compilation options. (don't recall - RHFM).

otherwise gcc will do the trick with __inline__.

MS has __forceinline (I have only read about this never used it.)

so you have to do something linke

#ifdef WIN32
// or maybe extent to win64 or make more macro ifs.
#define STRONGER_INLINE_HINT __forceinline
#else
#define STRONGER_INLINE_HINT __inline__
#endif

Well maybe STRONGER_INLINE_HINT is a bit long ...

/Thorbjørn
 
K

kceiw

Thomas Tutone 写é“:
Your example wouldn't result in the member function being declared
inline, as the OP specified. I think perhaps you meant something like:

class Example {
public:
void memFunc() const;
};

inline void Example::memFunc() const
{
doSomething();
}

Best regards,

Tom

How about when the inline function are put outside the header file? When
I try to do it, it make an error while linking.
Do I have to put the function body in the header file when I want to
make it inline? Dose it differ when I define the function while it's
declared?
 
T

tmartsum

2 possibilities:

1) Then it is (probably) not inlined.
The function is however in 2 more objectfiles.

2) You implemented the function inline in
file1.cpp
and
file2.cpp uses the inline function, but how
should file2.cpp see a function that should be inline and exists in
file1.cpp

gcc has an option to force export of inlined functions, but ofcourse
they
will then NOT be inlined in file2.cpp
 
G

Gabriel

How about when the inline function are put outside the header file? When
I try to do it, it make an error while linking.
Do I have to put the function body in the header file when I want to
make it inline? Dose it differ when I define the function while it's
declared?

As long as the declaration is visible when you call the function, you
should not get a linker error.

It does not make a differnce if you have separate definition/declaration
from a declaration/definition in one place. However, if the
_definition_ is not visible when you call the function, there is high
chance that the function will in fact not be inlined (This is very
difficult for the compiler/linker).

GAbriel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top