本文介绍了复合类问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我正在制作一个由C3DEngine类组成的3D引擎。这个

引擎的一部分是一个文件加载器,一个名为CMeshLoader的类。

我在C3DEngine中创建了一个CMeshLoader实例,即组合。

不幸的是,CMeshLoader有时需要将

引用给C3DEngine的私人成员。继承,朋友和作曲都在我脑海里旋转着。我知道

继承不是解决方案。有人可以建议一个优雅的方式

解决这个问题吗?


干杯

dave :)

解决方案



$ b在C3DEngine类中$ b,那么只需要提供读取器来读取

私有成员,并匹配setter来编写它们?

无论如何,一个类使用第二个需要来自第一个的数据,并不是很好吗?b $ b似乎真的适合我......?也许你应该想知道这个概念

以及为什么你的网格加载器需要来自引擎的数据,也许有一些方法来外化或重构部分代码使其更直观。





Dave-


我必须同意Jolly Green Giant(Le Geant Vert)。

将程序分区为输入,处理,和输出是我们如何写'

Fortran程序回到'60'。使用面向对象的编程,

对象应该处理它自己的初始化。在你的情况下,这个

表示C3DEngine应该有一个方法,而不是一个单独的类,

读取输入数据。如果你需要一个插件来处理不同的数据

格式,你可以尝试模板或多态。你可能想通过Design Patterns一书来获取想法。祝你好运。


-

Cy




,只需提供读取器来读取
私有成员,并匹配setters写它们?
无论如何,一个使用第二个需要来自第一个数据的类,



看起来不适合我......?也许你应该想知道
概念以及为什么你的网格加载器需要来自引擎的数据,也许有一些方法来外化或重构部分代码以使其更多
直观。




嗯,肯定会有一个包含在第一个需要的

类/对象的绝对罚款的情况下来自那里的数据。想象一下

例如一个CUniverse类包含一个CPlanet对象。例如CPlanet对象在模拟3体问题时从

CUniverse获得当前时间,这绝对是好的。原则上我也认为
认为提供访问功能是最好的主意。


问候

Chris

Hi

I''m making a 3D Engine which consists of the class C3DEngine. Part of this
engine is a file loader, a class called CMeshLoader.
I have made an instance of CMeshLoader in C3DEngine, ie composition.
Unfortunately, CMeshLoader sometimes needs to refer
to private members of C3DEngine. Inheritance, friends, and composition are
all swirling around in my head at the moment. I know that
inheritance is not the solution. Can someone please suggest an elegant way
of solving this problem?

Cheers
dave :)

解决方案



in the class C3DEngine, what about just supplying getters to read the
private members, and matching setters to write them ?
anyway, a class using a second one which needs data from the first, doesn''t
seem really right for me.... ? maybe you should wonder about the conception
and WHY does your mesh loader need data from the engine, and maybe there''s
some way to externalize or refactor parts of code to make it more intuitive.




Dave-

I have to agree with the Jolly Green Giant (Le Geant Vert) on this one.
Partitioning the program into input, processing, and output was how we wrote
Fortran programs back in the ''60''s. With object oriented programming an
object should take care of its own initialization. In your case this
indicates that C3DEngine should have a method, not a separate class, for
reading the input data. If you need a plugin to deal with different data
formats you can try templates or polymorphism. You might want to wade
through the Design Patterns book for ideas. Good luck.

--
Cy
http://home.rochester.rr.com/cyhome/




in the class C3DEngine, what about just supplying getters to read the
private members, and matching setters to write them ?
anyway, a class using a second one which needs data from the first,


doesn''t seem really right for me.... ? maybe you should wonder about theconception and WHY does your mesh loader need data from the engine, and maybe there''s
some way to externalize or refactor parts of code to make it moreintuitive.



Well, there are certainly cases when it is absolutely fine that a
class/object that is included in a first one needs data from that. Imagine
for example a CUniverse class the encloses a CPlanet object. It''s absolutely
fine that for example that CPlanet object obtains the current time from
CUniverse during the simulation of a 3 body problem. In principle I also
think that supplying access functions is the best idea.

Regards
Chris


这篇关于复合类问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 23:45