Skip to main content
gamedev.net
🔒 Locked

inline functions

Started by _elmo Feb 2, 2003 at 10:58 AM 5 replies 1.2k views
Original Post
_elmo
_elmo
I know where to use the inline keyword, but what is the meaning of an inline function...? Makes this the function faster or does it take less memory...? Please can anyone tell me the meaning of the inline keyword, and when to use it...?! Thanx... _elmo nieuwe schoenen...
elmo
Spearhawk
Spearhawk
The inline keywoard does exactly what it says it does, inlines.
e.g:


inline void PrintHello()
{
cout << "Hello" << endl;
}

int main()
{
PrintHello();
}

// Becomes when compiling
int main()
{
cout << "Hello" << endl;
}


It''ll be a tad faster in that that it eliminates the function call (very smal amount of time, but can be important if called offten). The bad thing with it is that the code gets longer, e.g bigger exes.
_elmo
_elmo
Thanx spearhawk...
elmo
Miserable
Miserable
It''s basically the C++ version of a C macro, meaning that it ideally incurs no function call overhead (that is, its text is pasted into the calling code, if the compiler does inline it), but with considerable improvements: Type safety, for one thing, and for another, it''s a real function, so you can do things like recursion (though your compiler might not inline a recursive function - it makes sense only if it can deduce the recursion depth at compile time, and it is not too great) and taking its address.
Brian Jones
Brian Jones
But remember, the inline keyword is a hint, not a request. The compiler can choose not to inline the function if it doesn''t want to.

I don''t have a signature
I don't have a signature
_elmo
_elmo
Sometimes the inline function is used in classes... Will this means that the whole class is placed where the function is called???

class AClass {
...
public:
inline void doSomething() {
i++;
}
};


int main(void) {
AClass *aclass = new AClass();

aclass->doSomething();

return 0;
}
elmo
Fruny
Fruny
quote:
Original post by _elmo
Sometimes the inline function is used in classes... Will this means that the whole class is placed where the function is called???



No, still only the member function. And anyway, you *already* have an instance of the class (in your case aclass).

Also, know that member functions fully defined in the body of the class (i.e. with the code for the function written within the class itself), are implicitely declared inline. The keyword is redundant.


[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.