| View previous topic :: View next topic |
| Author |
Message |
Guest
|
Posted: Sun Feb 26, 2006 7:06 am Post subject: Putting implmentation in .h files? |
|
|
Hi,
Can you please tell me what is the guideline in Putting implmentation
in .h files?
I see examples where they put the implementation of getter/setting in
the .h files where funcitons is > 10 lines of code are put in .cpp
file.
Is that the guideline?
Thank you. |
|
| Back to top |
|
 |
Alex Beluga Guest
|
Posted: Sun Feb 26, 2006 8:06 am Post subject: Re: Putting implmentation in .h files? |
|
|
Hello!
I see two cases as to where you put the implementation in .h file:
1. When you have a template class and you don't want to mess with pImpl
stuff.
2. When you have really small and calling very often functions that you
will put into a declaration of class. Please, consider that every
function that is in decl. of class is automaticallly an inline.
class foo
{
public:
int bar(); //declared somewhere else
void haha() //this is automatically inline
{
//some small code
}
}; |
|
| Back to top |
|
 |
Backer Guest
|
Posted: Sun Feb 26, 2006 10:06 am Post subject: Re: Putting implmentation in .h files? |
|
|
Normally the decleraions are made in the header files and implentaions
are in cpp file. But templates should be declared and implement in the
header file itself. |
|
| Back to top |
|
 |
Stephan Rupp Guest
|
Posted: Sun Feb 26, 2006 11:06 am Post subject: Re: Putting implmentation in .h files? |
|
|
Backer wrote:
| Quote: | Normally the decleraions are made in the header files and implentaions
are in cpp file. But templates should be declared and implement in the
header file itself.
|
.... or you hide away the implementation of a template's declarations in
a separate .inl file and include it with a last statement in the
template's header. So, you can emulate separation of interface and
implementation details for templates - an approach I usually prefer and
recommend.
Greets |
|
| Back to top |
|
 |
Martin Vejnar Guest
|
Posted: Sun Feb 26, 2006 12:06 pm Post subject: Re: Putting implmentation in .h files? |
|
|
<OT>
Alex Beluga wrote:
| Quote: | [snip]
class foo
{
public:
int bar(); //declared somewhere else
void haha() //this is automatically inline
{
//some small code
}
};
|
I was amused a few days ago, when I found out, that metasyntactic
variables have been mentioned even in RFC (RFC 3092, it's dated 1st
April 2001; coincidence? ) )
They list "standard" metasyntactic variables in the order they "should"
be used in code examples: foo, bar, baz, qux, quux, corge, grault,
garply, waldo, fred, plugh, xyzzy, thud.
So, correctly, your example should go like this:
class foo
{
public:
int bar(); //declared somewhere else
void baz() //this is automatically inline
{
//some small code
}
};
;o)
</OT> |
|
| Back to top |
|
 |
Alex Beluga Guest
|
Posted: Sun Feb 26, 2006 12:06 pm Post subject: Re: Putting implmentation in .h files? |
|
|
Thanks a lot! I think we should post this request in the neighbourly
residing comp.std.c++ or at least try to push it into Boost.
8-) |
|
| Back to top |
|
 |
Alex Beluga Guest
|
Posted: Sun Feb 26, 2006 12:06 pm Post subject: Re: Putting implmentation in .h files? |
|
|
Backer wrote:
| Quote: | Normally the decleraions are made in the header files and implentaions
are in cpp file. But templates should be declared and implement in the
header file itself.
|
.... or you could use the pImpl idiom like Boost-developers. |
|
| Back to top |
|
 |
Roland Pibinger Guest
|
Posted: Sun Feb 26, 2006 5:06 pm Post subject: Re: Putting implmentation in .h files? |
|
|
On Sun, 26 Feb 2006 12:05:25 +0100, Stephan Rupp
<alustr@tu-harburg.de> wrote:
| Quote: | ... or you hide away the implementation of a template's declarations in
a separate .inl file and include it with a last statement in the
template's header. So, you can emulate separation of interface and
implementation details for templates - an approach I usually prefer and
recommend.
|
Your approach may be usful but it neither hides the implementation of
templates nor emulates the separation of interface and implementation
for templates.
Best wishes,
Roland Pibinger |
|
| Back to top |
|
 |
Kai-Uwe Bux Guest
|
Posted: Sun Feb 26, 2006 6:06 pm Post subject: Re: Putting implmentation in .h files? |
|
|
Plissken.s (AT) gmail (DOT) com wrote:
| Quote: | Hi,
Can you please tell me what is the guideline in Putting implmentation
in .h files?
I see examples where they put the implementation of getter/setting in
the .h files where funcitons is > 10 lines of code are put in .cpp
file.
Is that the guideline?
|
There is no universal guideline. It all depends on the project and the
community/culture. The technical reason for separating interface and
implementation is to ease separate compilation, which reduces build times
in large projects. For small projects, I do not see a technically
compelling reason at all to separate definition and declaration. From a
programmers point of view, the only reasons that remain are about meeting
the reasonable expectations regarding code layout of your fellow
programmers and your future self. Those expectations tend to vary from
project to project.
Best
Kai-Uwe Bux |
|
| Back to top |
|
 |
Ian Collins Guest
|
Posted: Sun Feb 26, 2006 7:06 pm Post subject: Re: Putting implmentation in .h files? |
|
|
Backer wrote:
| Quote: | Normally the decleraions are made in the header files and implentaions
are in cpp file. But templates should be declared and implement in the
header file itself.
Please quote what you are replying to. |
Many compilers support template implementation in separate source files
that don't have to be included.
--
Ian Collins. |
|
| Back to top |
|
 |
Alex Guest
|
Posted: Sun Feb 26, 2006 7:06 pm Post subject: Re: Putting implmentation in .h files? |
|
|
Hello
What's pImpl stuff?
Thanks. |
|
| Back to top |
|
 |
Ian Collins Guest
|
Posted: Sun Feb 26, 2006 9:06 pm Post subject: Re: Putting implmentation in .h files? |
|
|
Roland Pibinger wrote:
| Quote: | On Mon, 27 Feb 2006 07:34:47 +1300, Ian Collins <ian-news (AT) hotmail (DOT) com
wrote:
Many compilers support template implementation in separate source files
that don't have to be included.
Which?
|
Sun and last time I used them, Microsoft and at least one embedded
compiler I've used.
There are rules for the name and location of the source file, but they
don't have to be included.
--
Ian Collins. |
|
| Back to top |
|
 |
Roland Pibinger Guest
|
Posted: Sun Feb 26, 2006 9:06 pm Post subject: Re: Putting implmentation in .h files? |
|
|
On Mon, 27 Feb 2006 07:34:47 +1300, Ian Collins <ian-news (AT) hotmail (DOT) com>
wrote:
| Quote: | Many compilers support template implementation in separate source files
that don't have to be included.
|
Which? |
|
| Back to top |
|
 |
osmium Guest
|
Posted: Sun Feb 26, 2006 10:06 pm Post subject: Re: Putting implmentation in .h files? |
|
|
"Alex" writes:
| Quote: | What's pImpl stuff?
|
It's called a "desing pattern". Try this search in google:
<"design pattern" pimpl> |
|
| Back to top |
|
 |
Alex Beluga Guest
|
Posted: Sun Feb 26, 2006 10:06 pm Post subject: Re: Putting implmentation in .h files? |
|
|
| Quote: | Many compilers support template implementation in separate source files
that don't have to be included.
|
Well, I'd rather not think of them because export of template is such a
horribly slow thing that it's better for compiling speed (what it's all
about) to keep source in headers.
Sincerely yours, Aleksander Beluga. |
|
| Back to top |
|
 |
|