[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

Which is better place to define functions

Ravi

11/26/2008 12:52:00 PM

I can define functions within the class body as well as outside by
using the scope operator. Which is the better place to do so? In Java
we do it it within the class body so the former method seems more
logical to me.

Another related question is that experienced C++ programmers use
separate header files for class definition and method definitions.
What will be the loss if I make class definitions and function
definition in the same file and include it in other files?

I am an experienced C programmer who has a little experience of Java
and started learning C++, so many concepts are new to me.
7 Answers

maverik

11/26/2008 1:11:00 PM

0

On Nov 26, 3:51 pm, Ravi <ra.ravi....@gmail.com> wrote:
> I can define functions within the class body as well as outside by
> using the scope operator. Which is the better place to do so?

It depends. You can define it within the class body it function body
is small or trivial. If function body is big it's better to define
outside of the class.

Also, there are many techniques requiring one of this method. If, for
example, you decide to separate definition from implementation, you
probably should define functions outside.

So there is no right answer at all.

> In Java
> we do it it within the class body so the former method seems more
> logical to me.
>
> Another related question is that experienced C++ programmers use
> separate header files for class definition and method definitions.
> What will be the loss if I make class definitions and function
> definition in the same file and include it in other files?

The first thing I can think of is that you need to rebuild all modules
that depends of this file. E.g. yo have

BaseModule.h:

class MyBaseClass {
public:
int ReturnNumber() { return 42; }
};

MyModule001.h:
#include <BaseModule.h>

....

MyModule999.h:
#include <BaseModule.h>
....

And some time later you decide to change ReturnNumber function:

int ReturnNumber() { return 0x0501; }

In this case you have to rebuild all modules [MyModule001 ...
MyModule999]

Of course, there are many other issues.

ebony.soft

11/26/2008 1:20:00 PM

0

On Nov 26, 3:51 pm, Ravi <ra.ravi....@gmail.com> wrote:
> I can define functions within the class body as well as outside by
> using the scope operator. Which is the better place to do so? In Java
> we do it it within the class body so the former method seems more
> logical to me.
>
> Another related question is that experienced C++ programmers use
> separate header files for class definition and method definitions.
> What will be the loss if I make class definitions and function
> definition in the same file and include it in other files?
>
> I am an experienced C programmer who has a little experience of Java
> and started learning C++, so many concepts are new to me.

Hi Ravi
Member function definition inside a class (and in a header file)
automatically makes function inline.
If you define a member function outside of a class and in a source
file (.cpp file
for example) without inline qualifier makes the function an ordinary
(non-inline)
function definition. You can use inline qualifier in header file and
outside of class definition too. The point is: place inline functions
inside header file and do not place ordinary function definition
inside headers.
Of course C++ programmers usually use header files for class
definitions and source files for their member function defintions.
Also I usually use inline for small and frequently used functions. BTW
sometimes how to declare/define classes/functions is a matter of
style.

Good luck
Saeed Amrollahi

Hel

11/26/2008 1:36:00 PM

0

On Nov 26, 1:51 pm, Ravi <ra.ravi....@gmail.com> wrote:
> I can define functions within the class body as well as outside by
> using the scope operator. Which is the better place to do so? In Java
> we do it it within the class body so the former method seems more
> logical to me.

If you think of it in terms of separating the interface from the
implementation, it's more logical to do it in separate files. If, for
instance, all you want is to find the declaration of a member
function, cluttering the interface with hundreds of lines of
implementation is just annoying.

> Another related question is that experienced C++ programmers use
> separate header files for class definition and method definitions.
> What will be the loss if I make class definitions and function
> definition in the same file and include it in other files?

If the functions are not declared inline (which they are, as Saeed
pointed out, if they are defined /inside/ the class definition), you
will probably get a linker error about the functions being multiply
defined if you include the same class definition more than once.

Greets, Hel

Matthias Buelow

11/26/2008 2:02:00 PM

0

Ravi wrote:

> I can define functions within the class body as well as outside by
> using the scope operator. Which is the better place to do so? In Java
> we do it it within the class body so the former method seems more
> logical to me.

Since C++ is lacking a proper module system, you'd better not make
header files too fat or you'll have to cope with astronomical compile
times. Another thing is that you can split your code over several files
as you like it, it gives a little more flexibility. Of course, in Java
you can't not use classes and everything has to be in a class, and these
restrictions allow for a straightforward class-file based module system.
It doesn't really make sense to compare Java and C++ here because
they're very much different in that regard.

> Another related question is that experienced C++ programmers use
> separate header files for class definition and method definitions.

What do you mean by that?

> What will be the loss if I make class definitions and function
> definition in the same file and include it in other files?

Isn't that exactly the same question as above?

> I am an experienced C programmer

Aha.

Jeff Schwab

11/26/2008 4:02:00 PM

0

Ravi wrote:
> I can define functions within the class body as well as outside by
> using the scope operator. Which is the better place to do so? In Java
> we do it it within the class body so the former method seems more
> logical to me.
>
> Another related question is that experienced C++ programmers use
> separate header files for class definition and method definitions.
> What will be the loss if I make class definitions and function
> definition in the same file and include it in other files?
>
> I am an experienced C programmer who has a little experience of Java
> and started learning C++, so many concepts are new to me.

I define almost everything in the header files, right in the class
definitions. I also make almost everything a template. The STL is
usually implemented in a similar fashion. My compile times are pretty
quick. I use no forward-declarations, other than those in headers
included from other libraries.

If your code is mostly templates, just stick it all in the same
translation unit and be done with it. Otherwise, your compile times
could explode, but I wouldn't worry to much about that unless it
actually becomes a problem for you.

G Kettleborough

11/26/2008 4:13:00 PM

0

On 26/11/08 12:51, Ravi wrote:
> I can define functions within the class body as well as outside by
> using the scope operator. Which is the better place to do so? In Java
> we do it it within the class body so the former method seems more
> logical to me.
>
> Another related question is that experienced C++ programmers use
> separate header files for class definition and method definitions.
> What will be the loss if I make class definitions and function
> definition in the same file and include it in other files?
>
> I am an experienced C programmer who has a little experience of Java
> and started learning C++, so many concepts are new to me.

Well a concept which you should be used to from C is keeping interface
separate from implementation. Usually you put the interface in a header
file (.h) and include this in all .cc files that you need it in. All
implementation stays in .cc files. What this means is you declare
classes and functions in .h files and then implement them in .cc files.

One advantage of this is that anyone using your class does not need to
know the implementation of your class, they only need to know the
interface. The implementation can change freely but the interface
remains the same.

Another advantage is that things can be built separately. If you include
entire implementations in every file then you will have to rebuild your
entire project for each small change that you make. The UNIX tool make
helps you with this. If you have main.cc which uses a class defined in
class.h and implemented in class.cc, main.cc will include class.h so
that you can use the class. Then main.cc and class.cc can be built
separately. If you make a change to main.cc there is no need to rebuild
class.cc.

This is a bit different if you use templates though because you need to
include the entire template implementation whenever you use it (because
the compiler needs to see the whole code in order to instantiate it).
There are other ways to include template code, but this is the simplest
and most popular.

--
George Kettleborough

James Kanze

11/26/2008 6:03:00 PM

0

On Nov 26, 1:51 pm, Ravi <ra.ravi....@gmail.com> wrote:
> I can define functions within the class body as well as
> outside by using the scope operator. Which is the better place
> to do so? In Java we do it it within the class body so the
> former method seems more logical to me.

In general, you don't want the function definitions (an
implementation detail) in the same file as the class definition
(which client code has to see); this is a well-known (and
serious) defect of Java (and C++ templates).

> Another related question is that experienced C++ programmers
> use separate header files for class definition and method
> definitions.

Not separate header files: they put the class definition in a
header file, because it is needed by the client code. Function
definitions are just an implementation detail; the client code
never sees them.

> What will be the loss if I make class definitions and function
> definition in the same file and include it in other files?

You'll get the same unmaintainable mess you get in Java. It can
be made to work for smaller projects, but as soon as several
people start working on a project, it becomes a real hassle.
(The classical work-around in Java is to define an interface for
everything, and only furnish it. This doesn't work in C++
because it requires dynamic allocation, and most of the time,
you don't want to dynamically allocate yor objects in C++.)

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34