Re: Defining many classes in a single file
From: Thomas Matthews (Thomas_MatthewsSpitsOnSpamBots_at_sbcglobal.net)
Date: 07/14/04
- Next message: piyush: "IPC mechanism Named Pipes or windows messages or sthg else ?"
- Previous message: Rob Williscroft: "Re: boost::spirit::actor doesn't accept a std::vector<int>"
- In reply to: Aguilar, James: "Defining many classes in a single file"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Wed, 14 Jul 2004 14:30:13 GMT
Aguilar, James wrote:
> I know that one can define an essentially unlimited number of classes in a
> file. And one can declare just as many in a header file. However, the
> question I have is, should I?
>
> Suppose that, to use the common example, I have a situation where I am
> implementing many types of Shapes. My current way of thinking is, well,
> since they are all the same type, let's just put them all in the same file.
> The include file would be "shapes.h" and it would contain not just the base
> class Shape, but also Curve, Arc, Circle, Polygon, Quadrilateral, Rhombus,
> Square, Octagon, Pentagon, etc.
>
> I am in a situation where I would potentially be creating hundreds of
> subclasses, most of which would inherit more than ninety percent of their
> functionality from parent classes. However, I do not know of any design
> alternative that would not require me to define all these various classes
> and subclasses, since certain ones of the subclass would require some very
> specialized behavior. Also, I intend that this system of "Shapes" would be
> very extensible.
>
> I had thought of busting out a .dat file and reading it during the
> initialization phase of my program to describe the behavior of these
> objects. However, I am inexperienced, to say the least, and I'm not sure a
> scripting language to describe behavior is the best use of time on a project
> that is pretty small.
>
> So, I want to avoid an excessive number of source files, but I want a lot of
> classes. Do I have the right solution?
>
>
I usually have one one class declared in a header file and
defined in a source file. I create directories / folders
to hold classes by theme. This helps with the maintenance
by having only one place to make one change. For example,
if I have class A & class B in a file, then if I change
class A, class B must also be recompiled. I like the
fact that I can get a class working, then not have to
recompile it due to changes in a non related class.
Another reason for one class one file is due to linker
resolution. Many Linkers have module scope as their
smallest resolution. If I have many classes defined
in a file and only want one, the linker will include
all the code for all the classes in that file (module
resolution). However, if I define only one class per
file, then I can only include the class I need and
not worry about extra code that I don't need.
In another thread, you ask about have many classes.
In this case, you can have libraries. Libraries are
collections of "object" files (the intermediary file
produced after compilation, but before linking).
This often speeds up the build process for classes
that don't change often. Many linkers support finer
resolution for library files than individual modules.
This may also alleviate symbol table overflow for
larger projects.
If you need multiple instances of classes at runtime
or the quantity varies at runtime, perhaps a factory
pattern is better suited. For example, one might
want a single Polygon class rather than pentagon,
hexagon, septagon and octagon classes. In this
case, you could create a hexagon instance by
specifying a 6 sided Polygon. All depends on your
project and your resources.
On the other hand, for small toy projects, I define
classes in one file until the quantity becomes
unwieldly. The effort and maintenance for multiple
files outweighs the time spent on the whole project.
--
Thomas Matthews
C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
- Next message: piyush: "IPC mechanism Named Pipes or windows messages or sthg else ?"
- Previous message: Rob Williscroft: "Re: boost::spirit::actor doesn't accept a std::vector<int>"
- In reply to: Aguilar, James: "Defining many classes in a single file"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|