Re: Which is better place to define functions
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=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34