转发声明包括,在声明之上include(ClassFwd.h + Class.h) [英] Forward declaration include, on top of declaration include (ClassFwd.h + Class.h)

查看:231
本文介绍了转发声明包括,在声明之上include(ClassFwd.h + Class.h)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在有效C ++(第三版)中,Scott Meyers在第31项中建议,类在经典的声明(.h)和定义(.cpp)文件之上应该有一个向前声明包含文件(fwd。 h),这个类不需要完整的定义可以使用,而不是向前声明自己。

In Effective C++ (3rd edition), Scott Meyers, in Item 31, suggests that classes should have, on top of their classic Declaration (.h) and Definition (.cpp) files, a Forward Declaration Include File (fwd.h), which class that do not need the full definition can use, instead of forward declaring themselves.

我有点看到它的情况,但我真的不看到这是一个可行的选择...它似乎很难维护,而不是过度,而且几乎不必要。

I somewhat see the case for it, but I really don't see this as a viable option... It seems very hard to maintain, rather overkill and hardly necessary.

然而,我可以看到它的用途模板转发声明,它们相当重。但是对于简单的类?它似乎是一个痛苦的维护,并将创建一个几乎空的包含文件,服务于一个非常小的目的...是值得的麻烦吗?

I can, however, see its use for template forward declarations, which are rather heavy. But for simple classes? It seems to be that it's a pain to maintain and will create a whole lot of almost empty include files that serve a very small purpose... is it worth the hassle?

下面是一个例子:

// Class.h
class Class
{
    Class();
    ~Class();
};

// ClassFwd.h
class Class;

// Class.cpp
Class::Class()
{
}

Class::~Class()
{
}

我的问题

你们认为什么?如果这是一个好的做法?

What do you guys think? If this a good practice?

注意我最感兴趣的是这个做法的参数,看看我是否错过了会让我同意Scott Meyers。

NOTE I am mostly interested in the arguments FOR this practice, to see if I missed something that would make me agree with Scott Meyers.

推荐答案

我对所有的库使用了前向声明头文件。库通常具有以下结构:

I used forward declaration header files for all my libraries. A library would typically have this structure:

lib/
  include/
    class headers + Fwd.h
src/
  source files + internal headers

lib / include 目录将包含所有公共类头文件以及一个前向声明头文件。这使得图书馆在包含侧轻量级。该库外的任何头只包括前向头( Fwd.h ),而该库外的源包括必要的完整头。还可以提供一个方便的标题( Lib.h ),其中包括所有其他标题,供在源文件中使用。

The lib/include directory would contain all public classes headers along with one forward declarations header. This made the library light-weight on the include side. Any header outside of this library only includes the forward header (Fwd.h), while sources outside of this library includes the necessary complete headers. One can also provide a convenience header (Lib.h) that includes all the other headers, for use in source files.

shared_ptr typedef 的另一件事,继承层次结构,返回指向实现的指针的工厂类。

Another thing to place in the forward declaration header is typedefs for shared_ptr, especially in the case of an inheritance hierarchy with factory classes that return pointers to implementations.

上面的代码对于有大量内部库的大型应用程序很有用。对于这种情况,上面的细化将在 lib / include / lib 中放置公共头。这样,您的库的客户端必须包括 lib /...。将此视为标头的命名空间。

The above is useful for larger applications with lots of internal libraries. A refinement of the above for this case would be to place the public headers in lib/include/lib. This way clients of your library would have to include lib/.... Think of this as a namespace for your headers.

祝你好运!

这篇关于转发声明包括,在声明之上include(ClassFwd.h + Class.h)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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