为什么成员类型不需要转发成员类型?

问题描述 投票:2回答:1
struct A
{
    void f1()
    {
        f2(); // ok, though f2() is not declared before
    }

    void f2()
    {}

    void f3(X*) // error: unknown type name 'X'
    {}

    struct X
    {};
};

int main()
{
    A a;
}

为什么成员类型不需要成员函数而需要向前声明?

c++ class types standards member-functions
1个回答
3
投票

这与complete-class context有关。当您处于成员函数的主体中时,该类将被视为完整的,并且可以使用该类中定义的任何内容,无论在类中的何处声明它。

函数参数不是该上下文的一部分,因此它们必须是在您尝试使用它们时就已知的类型。

© www.soinside.com 2019 - 2024. All rights reserved.