gpt4 book ai didi

c++ - 类中类

转载 作者:太空宇宙 更新时间:2023-11-03 10:27:23 24 4
gpt4 key购买 nike

我正在编写一个 3d Assets 导入库。 (它使用 Assimp ,顺便说一句)。有一个大场景,其中包含包含网格的节点,每个网格包含一种 Material 。所以我创建了下一个类:场景、网格、 Material 。

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

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

我的问题是是否有其他编码方法可以实现我的目标。

最佳答案

您可以查看 pimpl idiom .基本上,只公开客户端应该和可以在公共(public)接口(interface)中使用的内容,并将其他所有内容抽象掉:

// 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.
};

关于c++ - 类中类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28968532/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com