By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,759 Members | 959 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,759 IT Pros & Developers. It's quick & easy.

Body of a function in a header

P: n/a
My program is heavily template based, and I use the VC++, so I have to keep
the templates in the header file. My .cpp files are quite empty (they are
more a connection between header files). Now, my "problem" is that I can't
put the body of classless functions in the header files. I normally "solve"
this problem using the "inline" keyword (inline functions must be in header
files, but the compiler doesn't have to "inline" inline functions). Is there
a better way to do it? (yes... I know... this is an estetical problem :-) )

Can someone tell me why a class can have the body of the methods "inline" in
the header while a classless function can't? Are there istorical reasons?

--- bye
Jul 22 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
"Massimiliano Alberti" <xa*****@geocities.com> wrote...
My program is heavily template based, and I use the VC++, so I have to keep the templates in the header file. My .cpp files are quite empty (they are
more a connection between header files).
This last statement (in parentheses) sounds like a bad idea to me. If your
headers have to be included in a certain order or in order to work some of
your headers require a translation unit to include other headers, it's bad.
If you need something in a header, include the other header in that header.
Now, my "problem" is that I can't
put the body of classless functions in the header files. I normally "solve" this problem using the "inline" keyword (inline functions must be in header files, but the compiler doesn't have to "inline" inline functions). Is there a better way to do it? (yes... I know... this is an estetical problem :-) )
Can someone tell me why a class can have the body of the methods "inline" in the header while a classless function can't?
Nonsense. You may declare any function 'inline' as long as you provide the
function body right there for the compiler to use.
Are there istorical reasons?


I think you're a bit confused there.

inline void foo() { } // doesn't matter where you put this

int main() {
foo();
}

Victor
Jul 22 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.