简体   繁体   English

在头文件中前向声明另一个命名空间的自定义类型

[英]Forward-declaring a custom type of another namespace in a header file

I am using an external library named custom (not the actual name) which defines many of its data types in its own namespace. 我正在使用一个名为custom (不是实际名称)的外部库,该库在其自己的名称空间中定义了许多数据类型。 Let's assume that the namespace is named as custom as well. 假设命名空间也被命名为custom

I am trying to make use of a datatype named DataType . 我正在尝试使用一个名为DataType的数据DataType DataType is a custom type defined using a typedef, and let's assume that it can be used in a .cpp file by including "custom/datatype.h" DataType是使用typedef定义的自定义类型,我们通过包含"custom/datatype.h"来假定它可以在.cpp文件中使用。

My code has the following setting. 我的代码具有以下设置。

action.h: action.h:

class Action
{
    Action() {}
    virtual ~Action() {}

    virtual void foo(custom::DataType*) const = 0;
    ...
}

some_action.h: some_action.h:

#include "action.h"

class SomeAction : public Action
{
    SomeAction() {}

    virtual void foo(custom::DataType*) const override;
    ...
}

some_action.cpp: some_action.cpp:

#include "some_action.h"
#include "custom/datatype.h"
...
void SomeAction::foo(custom::DataType*) const
{
    ...
}

Is there any proper way to forward-declare DataType in action.h and some_action.h other than including "custom/datatype.h" in action.h ? 是否有前瞻性声明的任何适当的方式DataTypeaction.hsome_action.h比包括其他"custom/datatype.h"action.h

Is there any proper way to define DataType in action.h and some_action.h other than including "custom/datatype.h" in action.h ? 有没有定义任何适当的方式DataTypeaction.hsome_action.h比包括其他"custom/datatype.h"action.h

You probably don't want to "define" DataType , but to "forward declare" it instead. 您可能不希望“定义” DataType ,而是“向前声明”它。 This allows you to make the compiler aware of a custom::DataType which will be defined later. 这使您可以使编译器知道custom::DataType ,稍后将对其进行定义。

You can achieve this as follows. 您可以按照以下步骤实现。 In action.h and some_action.h add the following forward declaration : action.hsome_action.h添加以下向前声明

namespace custom { class DataType; }

In the .cpp files where you're going to need the definition of DataType , include "custom/datatype.h" . 在需要定义DataType.cpp文件中,包括"custom/datatype.h"


If custom::DataType is a type alias (ie typedef ) , you need to include the header. 如果custom::DataType类型别名 (即typedef ,则需要包括标题。 There isn't a way of forward-declaring type aliases: see this question & answers for related information. 没有一种向前声明类型别名的方法:有关相关信息,请参阅此问题和解答

If DataType is a class type or an enum with an underlying integer type, then you can forward declare it in action.h : 如果DataType是类类型或具有基础整数类型的枚举,则可以在action.h对其进行转发声明:

namespace custom {
  class DataType1;

  enum DataType2 : int; 
}

The downside is that it may break when you update the library. 缺点是更新库时可能会中断。 Well written libraries may have a header full of nothing other than forward declarations, which you may use, so you can look for those (example: iosfwd ). 编写良好的库中的标头可能只包含前向声明,您可以使用它,因此您可以查找这些标头(例如iosfwd )。

Otherwise, there is no recourse from including the header you are given by the library writer. 否则,将无法获得包含库编写者给您的标头的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM