如何停止传播通过的声明,包括分层? [英] How to stop propagating declarations through hierarchical includes?

查看:165
本文介绍了如何停止传播通过的声明,包括分层?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当我做出的的.h 的头文件。一个问题在我脑海中:

Whenever i make a .h header file. A question comes to my mind:

如何停止通过分层传播的声明包括?

假设有下面这些文件:

foo.h中

#ifndef FOO_H
#define FOO_H

typedef int foo_t;

inline int foo() { return 1; }

class foo_c {};

#endif  /* FOO_H */

bar.h

#ifndef BAR_H
#define BAR_H

#include "Foo.h"

typedef foo_t bar_t;

inline int bar() { return foo(); }

class bar_c : public foo_c {};

#endif  /* BAR_H */

zoo.h

#ifndef ZOO_H
#define ZOO_H

#include "Bar.h"

typedef bar_t zoo_t;

inline int zoo() { return bar(); }

class zoo_c : public bar_c {};

#endif  /* ZOO_H */

在文件中的 zoo.h 的,我们可以访问声明的元素 foo_c foo_t 富(),和每一个变化的 foo.h中的将强制为重新编译 动物园.H

In file zoo.h, we can access to declared elements foo_c, foo_t, foo(), and every changes to foo.h will force to re-compile zoo.h

我知道我们可以移动实现来的的.cpp 的文件,但如何约codeS类中定义的的.h 的文件写的?我们怎样才能迫使程序员明确包括的 foo.h中的中的 zoo.h 的,如果他需要它?

I know we can move implementations to .cpp files, but how about codes written in class definitions in .h files? How can we force the programmer to explicitly include foo.h in zoo.h if he needs it?

更新:我有一个例子:在Qt中,当我有,并使用< QQueue> ,我都进不去的QList ,其中 QQueue 的QList 继承。我必须包括<&的QList GT; 明确。的(另外,我不知道它是如何做的,并在编译的时候它的作用)

UPDATE: I have a example: In Qt, when i include and use <QQueue>, i have no access to QList where QQueue is inherited by QList. And i have to include <QList> explicitly. (Also, i dont know how it done, and effect of it on compile time)

注:随意编辑我的问题,如果它有有关该问题的解释疲软

推荐答案

在C ++和C,停止传播的声明,你需要从公共接口删除它们,时期。他们走向执行。或少公开界面。

In C++ and C, "to stop propagating declarations" you need to remove them from public interface, period. Move them to implementation. Or to "less public" interface.

编译时间是目标之一。其他人则可移植性,可维护性。这也直接与松耦合有关。

Compilation time is one of goals. Others are portability, maintenability. Also this is directly related with loose coupling.

最流行的C ++技术,可以与你的类派生帮助是平普尔成语。派生实现类,包括相应的​​头付诸实施CPP和你的公共接口前瞻性声明的实施。你的用户会一无所知基类,就会知道你的实现只有名称。

The most popular C++ technique that can help with your class derivation is Pimpl idiom. Derive your implementation class, include corresponding header into implementation cpp and forward-declare implementation in your public interface. Your users will know nothing about base class and will know only the name of your implementation.

这是不可能停止传播,如果你想使用的typedef 的。但是,为了提供更好的便携性和可维护性作为Boost库有效地使用,您可以使用相同的方法:实现定义的类型(如的这个)。

It's not possible to stop propagation if you'd like to use typedef's. But to provide better portability and maintenability you can use the same approach as Boost libraries use effectively: implementation-defined type (e.g. this one).

每个接口设计扩展之间的权衡,的信息隐藏和简单(或工作)。如果你需要归档前两种使用更复杂的方法。可以提供两个公共接口:一个用于使用和另一个,更广泛的和较低的水平,可扩展性。

Each interface design is a tradeoff between extensibility, information hiding and simplicity (or effort). If you need to archive first two use more sophisticated approach. You can provide two public interfaces: one for usage and another one, much wider and lower-level, for extensibility.

这篇关于如何停止传播通过的声明,包括分层?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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