如何解决循环依赖? [英] How to solve a circular dependency?

查看:103
本文介绍了如何解决循环依赖?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是我有一个 global_variables.h 文件,需要知道这些文件:

- Debugger.h

- Window.h

- Input.h



我的问题是Input.h需要知道Window.h,需要使用它的几个函数,因此它也需要global_variables.h文件,现在我遇到了循环依赖。



我使用Global_Variables的原因。 h文件是因为我知道很多文件需要访问我的不同游戏引擎组件,所以我把它们放到了Global_Variables.h文件中,起初这似乎是一个好主意。



但是,因为我是初学者,所以我没有想到我现在面临的这个问题。 :/



以下是一些代码示例:



Global_Variables .h:

My problem is that I have a global_variables.h file which needs to know about the files :
- Debugger.h
- Window.h
- Input.h

My problem is that Input.h needs to know about Window.h, and needs to use several of it's functions and therefore it also needs the global_variables.h file, and now I have ran into a circular dependency.

The reason I'm using a Global_Variables.h file is because I knew many files would need to access my different game engine components, so I threw them into the Global_Variables.h file, which seemed like a good idea at first.¨

But, since I'm a beginner i didn't think of this problem that I'm now facing. :/

Here's some code examples :

Global_Variables.h :

#pragma once

#include "Debugger.h"
#include "Window.h"
#include "Input.h"

// The name of the game the engine is currently running.
static const std::string GAME_NAME = "SwEngine Testing";

// The debugging/logging and also error handling component of the engine.
static Debugger global_debugger;

// The window/rendering/graphics component of the engine.
static Window global_window;

// The input component of the engine.
// Handles events such as window closing, keyboard pressing and mouse moving.
static Input global_input;





输入类是新创建的,只有析构函数和构造函数以及一些注释。



那么,我该如何解决这个问题呢? :)



我尝试了什么:



尝试使用谷歌搜索但是对于如何解决这个问题没有真正找到任何好的答案。

所以,我还没有真正尝试过任何东西。



And the input class is newly created, only has a destructor and a constructor as well as some comments.

So, how should I go about solving this problem? :)

What I have tried:

Have tried googling but haven't really found any good answers on how to solve the problem.
So, I have not really tried anything yet.

推荐答案

有几种方法可以解决它。传统的老式游戏,最兼容的方式是使用所谓的包含守卫。



包括警卫 - 维基百科,免费的百科全书 [ ^ ]



基本上,如果Window.h需要确保包含Input.h和global_variables.h,那么程序的其他区域也是如此,然后使用警卫将确保编译只处理包含中的内容一次。



另外,使用 #pragma一次在你的所有标题中是另一​​种方式。不只是在主文件中。但这是某些编译器特有的,如Visual Studio,所以除非你不想切换编译器,否则不推荐使用。
There's a couple ways to go about it. The traditional old-skool, most compatible way is to use what's called an include guard.

include guard - Wikipedia, the free encyclopedia[^]

Basically, if Window.h needs to make sure Input.h and global_variables.h are in included, but so do other areas of the program, then using guards will ensure the compile only processes what's in the include once.

Also, using #pragma once in all of your headers is another way. Not just in a main file. But this is specific to certain compilers such as Visual Studio, so isn't recommended unless you have no desire to ever switch compilers.


这篇关于如何解决循环依赖?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆