类声明的循环依赖关系
Circular dependency of class delclarations
我有两个带有单独标题的类:class Renderer
和 class Texture
。Texture
实例旨在管理驻留在Renderer
内存池中的一些数据,因此Texture
实例不能独立于Renderer
实例而存在。要获取Texture
实例的句柄,只需使用正确的文件名调用Renderer::loadTexture
,Renderer
在其数据库中搜索具有匹配文件名Texture
对象,如果未找到匹配项,则实例化新纹理。
//renderer.h
#include "texture.h"
class renderer
{
public:
Texture* loadTexture(std::string fileName);
private:
std::map<std::string, Texture*> textureDb;
};
//texture.h
class Texture
{
public:
Texture(std::string imageFileName);
};
我突然想到,将class Texture
构造函数设为私有,并将Texture * Renderer::loadTexture(std::string filename)
声明为class Renderer
的朋友是合乎逻辑的:
//renderer.h
#include "texture.h"
class renderer
{
public:
Texture* loadTexture(std::string fileName);
private:
std::map<std::string, Texture*> textureDb;
};
//texture.h
class texture;
#include "renderer.h"
class Texture
{
private:
Texture(std::string imageFileName);
Texture(const Texture &);
friend Texture* loadTexture(std::string);
};
但是,只有在包含 renderer.h 之前始终包含 texture.h 时,这才会编译,因为class Texture
要求已经定义class Renderer
。防止这种情况的最佳方法是什么?两个文件中都存在包含保护,但为简洁起见,此处省略了。
首先,从上面的代码中,尽可能向前声明。这应该会有所帮助。
// Renderer.h
class Texture;
class Renderer {
...
};
// Renderer.cpp
#include "Renderer.h"
#include "Texture.h"
...
// Texture.h
//class Renderer; // looks like this isn't needed
class Texture {
...
};
// Texture.cpp
#include "Renderer.h"
#include "Texture.h"
...
使用前向声明,您应该有足够的时间来声明指向 Render er 类标头中的 Texture 指针。看起来这就是您此时所需要的;将其余部分推送到渲染器.cpp。
接下来,避开朋友;但是,在这种情况下可能是可以接受的。
最后,循环依赖从来都不是一件好事。看看你是否可以稍微改变一下你的设计。看看是否可以引入接口或抽象基类来打破循环依赖关系。看起来渲染器根本不需要依赖纹理。
相反,考虑重写渲染器以依赖于某种 ITexture 接口,然后让 Texture 实现 ITexture:
class ITexture
{
// no data members
// no method bodies
// only pure virtual method declarations
};
class Renderer
{
public:
ITexture* loadTexture(std::string fileName);
private:
std::map<std::string, ITexture*> textureDb;
};
class Texture : public ITexture
{
...
};
- C++GTKMM gui循环依赖关系
- 如何在头文件中声明类模板(由于循环依赖关系)
- 对在不同二进制文件中创建的对象文件的依赖关系
- 使用Bazel构建具有不同编译器/链接器选项的C/C++依赖关系
- OpenVINO - 推理库插件 libMKLDNNPlugin.so 无法解析依赖关系
- 模拟测试中类的依赖关系
- C++模板方法中的循环依赖关系
- 解析正交模块的依赖关系
- 如何在 Mac OS 上安装 boost-mpi 及其对 clang 的依赖关系?
- Wt::D bo 中的循环依赖关系
- 在包含窗口标头时难以解决循环依赖关系问题
- 当依赖关系和依赖关系都是多态时,在哪个继承级别存储依赖关系指针?
- 解决循环依赖关系 c++ 的想法
- C++循环依赖关系,未声明的标识符
- C++ 中的循环依赖关系问题
- 为什么包含需要进一步的依赖关系?
- 使用 cmake 获取外部依赖关系
- CMake 外部和内部静态库的循环依赖关系
- 在没有Xcode的macOS中开发具有依赖关系的应用程序
- "std::shared_ptr"循环依赖关系是如何导致问题的