避免头文件循环依赖 [英] Avoiding Circular Dependencies of header files

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

问题描述

你有如何避免头文件吗?

Do you have any good advice on how to avoid circular dependencies of header files, please?

当然,从一开始,我尝试设计项目尽可能透明。然而,随着越来越多的功能和类,并将获得项目不太透明,循环依赖开始发生。

Of course, from the beginning, I try to design the project as transparent as possible. However, as more and more features and classes are added, and the project gets less transparent, circular dependencies start happening.

是否有任何一般,验证和工作规则吗感谢。

推荐答案

如果您有循环依赖,那么你做错了什么。

If you have circular dependency then you doing something wrong.

至于例如:

foo.h
-----
class foo {
public:
   bar b;
};

bar.h
-----
class bar {
public:
   foo f;
};

是非法的,你可能想:

Is illegal you probably want:

foo.h
-----
class bar; // forward declaration
class foo {
   ...
   bar *b;
   ...
};

bar.h
-----
class foo; // forward declaration
class bar {
   ...
   foo *f;
   ...
};

这是确定的。

一般规则:


  1. 确保每个标题可以包含在它自己的。

  2. 如果声明中使用它们,您可以使用前进!

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

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