if i want to inline a member function do i need to have it in both places below(decleration and implementation section)? or can i just place it in implementation section only or decleration section only and get the same result?

class Ray {
public:
inline allocate( const int Lize );
{;

//implementation section
inline void Ray::allocate( const int Lize )
{
}

Recommended Answers

All 2 Replies

As long as the code you show is in your header file, you'll be ok. You can also just put the implementation directly into the class declaration.

Mostly for me, its a matter of aesthetics: If the inline function is a one- or two-liner, I tend to put it in the class declaration, but if more than that, usually in the implementation. Note that the inline keyword is a suggestion to the compiler which may decide not to inline it if in the compiler's opinion, it is better out of line. Branching and looping have a tendency to be "out-lined" by the compiler, and of course if you take the address of the function, it has to have a "place to be" so it can be pointed to, and the compiler will certainly lay code down out of line for that.

C++ Standard 7.1.2/4 and 3.2/3 (One-definition Rule):

An inline function shall be defined in every translation unit in which it is used.

In other words, all inline function (if the compiler so decides) have to be defined (have their implementation) in a header file (unless you want to get into the messy business of #including cpp files). And, any function that is defined in the header file has to be marked as inline, otherwise it will break the one-definition rule (even if the function clearly wouldn't be inlined by the compiler).

C++ Standard 9.3/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.( Note: member functions of a class in namespace scope have external linkage. Member functions of a local class (9.8) have no linkage. See 3.5. — end note )

So, either one is OK, and both are OK as well. It basically doesn't matter where it is, as long as it is there.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.