Sign in to follow this  
roos

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 this post


Link to 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


Link to 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


Link to post
Share on other sites
Quote:
Original post by roos
lso 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


Link to post
Share on other sites
Quote:
Original post by roos
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


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


Link to 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 prototype

inline 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


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this