Body of a function in a header

  • Thread starter Massimiliano Alberti
  • Start date
M

Massimiliano Alberti

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
 
V

Victor Bazarov

Massimiliano Alberti said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,160
Messages
2,570,889
Members
47,422
Latest member
LatashiaZc

Latest Threads

Top