xmllmx
xmllmx

Reputation: 42371

Why do member types need to be forward declared while member functions needn't?

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;
}

Why do member types need to be forward declared while member functions needn't?

Upvotes: 2

Views: 47

Answers (1)

NathanOliver
NathanOliver

Reputation: 180630

This has to do with the complete-class context. When you are in the body of a member function, the class is considered complete and can use anything defined in the class, no matter where in the class it is declared.

The function parameters are not part of that context so they must be types that are known about at the point you try to use them.

Upvotes: 5

Related Questions