类中的类

Pil*_*pel 1 c++

我正在写一个3d资产导入库.(它使用Assimp,顺便说一句).有一个包含网格的节点的大场景,每个网格都包含一个材质.所以我创建了下一个类:Scene,Mesh,Material.

只有Scene类应该被编码器实例化和使用,所以最合理的做法是(imo)将Mesh声明为一个私有的内部Scene(以及Material作为Mesh内部的私有).

这应该没问题,因为只有Scene才应该使用Mesh,但唯一的问题是它看起来很糟糕,而且我不方便用这种方式编码.(函数嵌套在类中嵌套的类等...)

我的问题是,是否有其他编码方法来实现我的目标.

Luc*_*ore 5

你可以看看pimpl成语.基本上,只公开客户端应该和可以在公共接口中使用的内容,并保留其他所有内容:

// 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.
};
Run Code Online (Sandbox Code Playgroud)