我正在编写一个3d资源导入库。(它使用Assimp,btw)。有一个大场景,其中包含包含网格的节点,并且每个网格都包含一种材质。所以我创建了下一个类: Scene,Mesh,Material。
只有Scene类应该由编码器实例化和使用,因此最合理的做法(imo)是将网格声明为私有内部场景(也将材质声明为私有内部网格)。
这应该很好,因为只有场景应该使用网格,但唯一的问题是它看起来很糟糕,而且对我来说用这种方式编码不方便。(嵌套在类中的函数嵌套在类中,等等)
我的问题是,是否有其他编码方法来实现我的目标。
发布于 2015-03-11 00:10:26
您可以查看pimpl idiom。基本上,只公开客户端应该和可以在公共接口中使用的内容,并保持其他所有内容的抽象:
// scene_interface.h
class SceneImpl; //only forward-declare
class Scene
{
// client-visible methods, and that's all
// no implementation details
private:
SceneImpl* pImpl; // <- look, the name
};
// scene_impl.h & scene_impl.cpp
// hidden from the client
class Mesh
{
//...
};
class SceneImpl
{
Mesh* pMesh;
//etc.
};发布于 2015-03-11 00:12:23
您可以在头(.h)文件中将私有类限制为不完整的声明,然后在实现(.cpp)文件中完整地定义它们。
标题Scene.h
class Scene
{
// ...
private:
class Node;
class Mesh;
Node *node;
Mesh *mesh;
};实施Scene.cpp
class Scene::Node
{
// ...
};
class Scene::Mesh
{
// ...
};
// definitions of member functions ...https://stackoverflow.com/questions/28968532
复制相似问题