Jump to content
• Advertisement

inline methods in VC++

This topic is 4839 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, Does anyone know how to make a method "inline" without putting the definition inside the declaration? For example usually to make something inline I do this:
// file: CWhatever.h

class CWhatever
{
void DoSomething()  { cout << "Doing something" << endl; }
...
};


Instead, I'd like to be able to do:
// file: CWhatever.h

class CWhatever
{
inline void DoSomething();
};

// file: CWhatever.cpp

inline void CWhatever::DoSomething()
{
}


I'd think this should work but it always results in unresolved external error. Edit: btw, in case anyone is wondering why I would prefer the 2nd way over the 1st, it's so that the class declaration stays "clean" and not cluttered. Thanks! roos

Share this post

Share on other sites
Advertisement
You might try taking out the second inline.

void CWhatever::DoSomething()

Don't know if that will fix it.

Share this post

Share on other sites
I don't know what's causing that error, but you don't want to do that anyhow. To inline a function, the code has to be available to the compiler. Without it, how would it know what to inline? Removing the code from the header means that other cpp files won't have access to the code until link time, when inlining is generally not done. *edit: if you insist on removing it from the header, include the source file at the end. I think that'll do the trick...it does for templates, at least.

Also, remember that inline is a hint, not a requirement. A modern compiler will inline functions you don't ask it to, and not inline functions you do ask it to, as it sees fit. Don't expect a cure all.

CM

Share this post

Share on other sites
Thanks guys. I think your explanation is correct, Connor. So I'll just put the definitions below the class declaration, but still in the header file as I do for templates. Also I have heard that inline is only a hint, but as far as I know, the compiler doesn't completely ignore it, right? In that case, it seems like inlining functions is still worth something.

roos

Share this post

Share on other sites
Quote:
 Original post by rooslso I have heard that inline is only a hint, but as far as I know, the compiler doesn't completely ignore it, right? In that case, it seems like inlining functions is still worth something.

At the very least it can't hurt. That was just a standard disclaimer, so that there's no confusion if you add inline to some functions and none of the actual generated code changes.

CM

Share this post

Share on other sites
Quote:
 Original post by roosThanks guys. I think your explanation is correct, Connor. So I'll just put the definitions below the class declaration, but still in the header file as I do for templates. Also I have heard that inline is only a hint, but as far as I know, the compiler doesn't completely ignore it, right? In that case, it seems like inlining functions is still worth something.roos

It might help you understand "inline" better if you read
Inline Redux and Guru of the Week #33 written by Herb Sutter.

Share this post

Share on other sites
If you inline in your .cpp file the inlined function will only effect any local functions that are within the scope of the .cpp file, not other .cpp files. If the function is in the header as a prototype it still will not be able to find the function in the object code because it's inlined it never really writes a function in the object code.

If you would like to make it clean you should put the inline code seperate in the header file (ignore the .cpp file). Basically like so :

class CWhatever{    void DoSomething();};// Add the function/method below your class prototypeinline void CWhatever::DoSomething(){}

This way the function/method is properly inlined to be used globally, and it stays outside of the prototype avoiding a messy looking class structure. I can't stand code that inlines a function in their prototype, it looks so ugly. C++ programmers need to learn etiquette. Anyway hope this helps.

Share this post

Share on other sites

• Advertisement
• Advertisement

• Popular Contributors

1. 1
2. 2
3. 3
Rutin
18
4. 4
JoeJ
12
5. 5
• Advertisement

• 14
• 9
• 22
• 9
• 31
• Forum Statistics

• Total Topics
632619
• Total Posts
3007491
• Who's Online (See full list)

There are no registered users currently online

×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!