在单个文件中定义许多类 [英] Defining many classes in a single file

查看:67
本文介绍了在单个文件中定义许多类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道可以在

文件中定义基本上无限数量的类。并且可以在头文件中声明一样多。但是,我有的问题是,我应该吗?


假设,使用常见的例子,我有一个情况,我是

实现了许多类型的形状。我现在的想法是,

,因为它们都是相同的类型,让我们把它们全部放在同一个文件中。

包含文件会是shapes.h并且它不仅包含基础

类形状,还包括曲线,弧形,圆形,多边形,四边形,菱形,

方形,八边形,五角大楼等


我的情况是,我可能会创建数百个

子类,其中大部分会继承超过百分之九十的
$父类的b $ b功能。但是,我不知道任何设计

替代方案,不需要我定义所有这些不同的类

和子类,因为某些子类需要一些非常

专业行为。此外,我打算使用这种形状系统。将是

非常可扩展。


我曾想过在

初始化阶段淘汰.dat文件并阅读它我的程序来描述这些

对象的行为。但是,至少可以说,我没经验,而且我不确定描述行为的

脚本语言是项目上时间的最佳利用

很小。


所以,我想避免过多的源文件,但我想要很多

类。我有正确的解决方案吗?

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?

推荐答案



" Aguilar,James" < JF ** @ cec.NOBOTSwustl.edu>在消息中写道

news:cd ********** @ newsreader.wustl.edu ...

"Aguilar, James" <jf**@cec.NOBOTSwustl.edu> wrote in message
news:cd**********@newsreader.wustl.edu...
我知道一个人可以定义一个基本上无限制的
文件中的类数。并且可以在头文件中声明一样多。但是,我的问题是,我应该吗?

假设,使用常见的例子,我有一种情况,即我实现了许多类型的形状。我现在的想法是,好吧,因为它们都是相同的类型,所以我们只需将它们全部放在同一个
文件中。包含文件将是shapes.h。它不仅包含
基类Shape,还包含曲线,弧形,圆形,多边形,四边形,菱形,方形,八边形,五角形等。

我在我可能会创建数百个子类的情况下,其中大部分将从父类继承其百分之九十以上的功能。但是,我不知道任何设计
替代方案,不需要我定义所有这些不同的类和子类,因为某些子类需要一些非常特殊的行为。此外,我打算使用这种形状系统。
是非常可扩展的。

我曾想过在我的程序的初始化阶段中清除一个.dat文件并读取它来描述这些行为。 >对象。但是,至少可以说,我缺乏经验,而且我不确定描述行为的
a脚本语言是对b $ b项目的最佳使用时间。

所以,我想避免过多的源文件,但我想要很多
的类。我有合适的解决方案吗?
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?




我对这个问题的看法(我不知道专家会不同意!):

关于实现文件,我对将所有

类的实现放入一个文件有所保留。您可以轻松地将

实现拆分为任意数量的文件,而不会牺牲最终用户的
可用性。您的类的用户可以继续只包含1个标题

文件而无需知道它们实现的文件。

如果代码行太多在1个实现文件中,它增加了维护的复杂性。最好是模块化的。


关于头文件,确实有一个约定你应该

每个头文件只声明一个类。但是有一些优先权(并不是说b $ b意味着它们必然是正确的,但它只是表明事情是这样的,而不是那么大的邪恶,因为它被证明是,如果你在头文件中放入多个类

声明)


我在这里给出了一个非常特定于平台的例子。 MFC库有很多例子,其中单个头文件声明了许多类(如果你看看他们的集合类,它们都是在afxcoll.h中声明的,

编号接近20.同样是普通控制类的情况,

编号接近20)。现在用户可以方便地包含

只有1个标题并开始使用多个类。如果构建时间有问题,那么可以使用一些编译器特定的功能来减少构建时间。

(VC ++中预编译的头文件和pch文件)

Senapathy



My take on this subject (I don''t know whether the experts would disagree!) :

About implementation files, I have my reservations about putting all the
classes'' implementations into one file. You can easily split the
implementation into any number of files without sacrificing on end-user''s
usability. The user of your classes can continue to include just 1 header
file of yours without needing to know which files they are implemented in.
If there are too many lines of code in 1 implementation file, it increases
the complexity of maintenance. Better to be modular.

Regarding header files, true that there is a convention that you should
declare only 1 class per header file. But there are precedences (doesn''t
mean that they are necessarily right, but it just indicates that things are
not so drastically evil as it is made out to be, if you put multiple class
declarations in a header file)

I am giving a very platform specific example here. The MFC library has
numerous examples where a single header file declares many classes (if you
take a look at their collection classes, they are all declared in afxcoll.h,
numbering close to 20. Same is the case for the common control classes,
numbering nearly 20). Now the user has the convenience that he can include
just 1 header and start using multiple classes. If build time is a problem,
some compiler specific features could be there to reduce the build time.
(pre-compiled headers and pch files in VC++)

Senapathy




" Senapathy" < SE ********* @ siemens.com>在消息中写道

news:cd ********** @ news.mch.sbs.de ...

"Senapathy" <se*********@siemens.com> wrote in message
news:cd**********@news.mch.sbs.de...

[snip]

[snip]




后续问题:


没有一大堆不同的源文件(比如说,放入一个

子目录)makefile是地狱吗?我可以想象一条规则说:


2dphysics.o:



Followup question:

Won''t having a whole bunch of different source files (say, put in a
subdirectory) be hell for the makefile? I can imagine a rule that says:

2dphysics.o:


(CCC)


这篇关于在单个文件中定义许多类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆