# inline methods in VC++

## 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 on other sites
You might try taking out the second inline.

void CWhatever::DoSomething()

Don't know if that will fix it.

##### 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 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 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 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

Inline Redux and Guru of the Week #33 written by Herb Sutter.

##### 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.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
627711
• Total Posts
2978744

• 21
• 14
• 12
• 36
• 30