473,387 Members | 1,504 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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.

Feb 26 '06 #1
17 1781
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
}
};

Feb 26 '06 #2
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.

Feb 26 '06 #3
Backer wrote:
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
Feb 26 '06 #4

Backer wrote:
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.

Feb 26 '06 #5
<OT>

Alex Beluga wrote:
[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? :o) )

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>
Feb 26 '06 #6
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-)

Feb 26 '06 #7
On Sun, 26 Feb 2006 12:05:25 +0100, Stephan Rupp
<al****@tu-harburg.de> wrote:
... 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
Feb 26 '06 #8
Pl********@gmail.com wrote:
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

Feb 26 '06 #9
Hello

What's pImpl stuff?

Thanks.

Feb 26 '06 #10
Backer wrote:
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.
Feb 26 '06 #11
On Mon, 27 Feb 2006 07:34:47 +1300, Ian Collins <ia******@hotmail.com>
wrote:
Many compilers support template implementation in separate source files
that don't have to be included.


Which?
Feb 26 '06 #12
Roland Pibinger wrote:
On Mon, 27 Feb 2006 07:34:47 +1300, Ian Collins <ia******@hotmail.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.
Feb 26 '06 #13
"Alex" writes:
What's pImpl stuff?


It's called a "desing pattern". Try this search in google:

<"design pattern" pimpl>
Feb 26 '06 #14
> 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.

Feb 26 '06 #15
Alex Beluga wrote:
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.

Not if the compiler knows where to find the source.

--
Ian Collins.
Feb 26 '06 #16
Ian Collins wrote:
Roland Pibinger wrote:
On Mon, 27 Feb 2006 07:34:47 +1300, Ian Collins <ia******@hotmail.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.


That's incorrect. There are only two comercial compilers that support
the C++ export keyword, and that's Comeau and Intel C++.
I would not call two compilers many.

I think you're mistaking the idea of being able to compile template
code and only using it in a single *.cpp file (translation unit), with
the idea of being able to use the template code in one *.cpp file
(translation unit), from another *.cpp file (translation unit).

Without a compiler supporting the export keyword, the only way you can
exteranlly use template code in a *.cpp file, is by declaring forward
template declaration within the file that contains the template code.
But then that limits you to only using the types that have been forward
declared.

Feb 27 '06 #17
Axter wrote:
Ian Collins wrote:
Roland Pibinger wrote:
On Mon, 27 Feb 2006 07:34:47 +1300, Ian Collins <ia******@hotmail.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.

That's incorrect. There are only two comercial compilers that support
the C++ export keyword, and that's Comeau and Intel C++.
I would not call two compilers many.

No, it's nothing to do with export.

Some compilers when they attempt to instantiate template code where the
definition isn't in the header have a set of search rules used to find a
matching cc/cpp file containing the code. Normally this defaults to a
file with the same name in the same directory. More like an implicit
include.

So id X.h contains

template <typename Y>
class X
{
X(int);
}

and X.cc contains

template <typename Y>
X:X( int ) {}

the compiler will find the constructor code in X.cc.
I think you're mistaking the idea of being able to compile template
code and only using it in a single *.cpp file (translation unit), with
the idea of being able to use the template code in one *.cpp file
(translation unit), from another *.cpp file (translation unit).
At least one compiler I use keeps a cache of instantiated templates and
checks this before instantiating, giving the same result.
Without a compiler supporting the export keyword, the only way you can
exteranlly use template code in a *.cpp file, is by declaring forward
template declaration within the file that contains the template code.
But then that limits you to only using the types that have been forward
declared.

Or in the example above, include the header.

--
Ian Collins.
Feb 27 '06 #18

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Eric Kincl | last post by:
Hello, its been a while since I posted/looked here... my normal email client doesn't handle newsgroups :( (ximian evolution) I was wondering how you stick a file into a database, and then...
1
by: Christopher M. Lusardi | last post by:
Hello, I have a program that can be compiled and run on SGI and Linux computers, and only one of the files in the program has is different. On the SGI system, the file has a dot capital C...
0
by: Alexey Zhilenko | last post by:
I have to perform this operation: Getting and putting files onto virtual folder on WEB site programmatically. Please advise me which component to use. Thank you.
1
by: Davy Marichael | last post by:
Hello, i'm working with ASP.NET to create my aspx files, the program i've made communicates with a Db (oledb) to read data and to write data.. I've found a server that supports asp,php,...
3
by: Asfand Yar Qazi | last post by:
For years, I've been putting everything that won't be needed outside a compilation unit in anonymous namespaces, even editing my old files that did things the 'old' way (using static linkage). ...
3
by: Kenneth McDonald | last post by:
Over the last couple of years, I've built a module called rex that lays on top of (and from the user's point of view, hides) the re module. rex offers the following advantages over re. *...
0
by: Anish G | last post by:
Hi, I have an issue with reading CSV files. I am to reading CSV file and putting it in a Datatable in C#. I am using a regular expression to read the values. Below is the code. Now, it reads...
0
by: drawing in aspnet | last post by:
Question about putting the data layer in a separate class library. I keep reading that the data layer should be separated from the presentation layer and put in its own class library. I am...
2
by: niskin | last post by:
I'm trying to create a file called retrieve.vbs with my program and it creates this file, with all the necessary code for the file to run, but the part that relies on user input does not get written...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.