我什么时候可以使用前向声明? [英] When can I use a forward declaration?

查看:34
本文介绍了我什么时候可以使用前向声明?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找何时可以在另一个类的头文件中对一个类进行前向声明的定义:

I am looking for the definition of when I am allowed to do forward declaration of a class in another class's header file:

我是否可以为基类、作为成员持有的类、通过引用传递给成员函数的类等执行此操作?

Am I allowed to do it for a base class, for a class held as a member, for a class passed to member function by reference, etc. ?

推荐答案

把自己放在编译器的位置上:当你向前声明一个类型时,编译器只知道这个类型存在;它对自己的大小、成员或方法一无所知.这就是为什么它被称为不完整类型.因此,您不能使用该类型来声明成员或基类,因为编译器需要知道该类型的布局.

Put yourself in the compiler's position: when you forward declare a type, all the compiler knows is that this type exists; it knows nothing about its size, members, or methods. This is why it's called an incomplete type. Therefore, you cannot use the type to declare a member, or a base class, since the compiler would need to know the layout of the type.

假设有以下前向声明.

class X;

以下是您可以做什么和不可以做什么.

Here's what you can and cannot do.

你可以用不完整的类型做什么:

  • 将成员声明为不完整类型的指针或引用:

  • Declare a member to be a pointer or a reference to the incomplete type:

class Foo {
    X *p;
    X &r;
};

  • 声明接受/返回不完整类型的函数或方法:

  • Declare functions or methods which accept/return incomplete types:

    void f1(X);
    X    f2();
    

  • 定义接受/返回指向不完整类型的指针/引用的函数或方法(但不使用其成员):

  • Define functions or methods which accept/return pointers/references to the incomplete type (but without using its members):

    void f3(X*, X&) {}
    X&   f4()       {}
    X*   f5()       {}
    

  • 不能用不完整的类型做什么:

    • 将其用作基类

    • Use it as a base class

    class Foo : X {} // compiler error!
    

  • 用它来声明一个成员:

  • Use it to declare a member:

    class Foo {
        X m; // compiler error!
    };
    

  • 使用这种类型定义函数或方法

    void f1(X x) {} // compiler error!
    X    f2()    {} // compiler error!
    

  • 使用它的方法或字段,实际上是试图解引用一个类型不完整的变量

  • Use its methods or fields, in fact trying to dereference a variable with incomplete type

    class Foo {
        X *m;            
        void method()            
        {
            m->someMethod();      // compiler error!
            int i = m->someField; // compiler error!
        }
    };
    

  • 说到模板,没有绝对的规则:是否可以使用不完整的类型作为模板参数取决于该类型在模板中的使用方式.

    When it comes to templates, there is no absolute rule: whether you can use an incomplete type as a template parameter is dependent on the way the type is used in the template.

    例如,std::vector 要求其参数是完整类型,而 boost::container::vector 则不需要.有时,只有在使用某些成员函数时才需要完整类型;这是std的情况:例如:unique_ptr.

    For instance, std::vector<T> requires its parameter to be a complete type, while boost::container::vector<T> does not. Sometimes, a complete type is required only if you use certain member functions; this is the case for std::unique_ptr<T>, for example.

    文档齐全的模板应在其文档中指明其参数的所有要求,包括它们是否需要是完整类型.

    A well-documented template should indicate in its documentation all the requirements of its parameters, including whether they need to be complete types or not.

    这篇关于我什么时候可以使用前向声明?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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