Search code examples
c++cyclic-reference

C++ class methods forward declaration


Is there any way to redeclare a class to define methods which where only declared this far?

Eg. something like:

class A
{
    void a();
    void b() {}
}

class A
{
    void a() {}
}

instead of

class A
{
    void a();
    void b() {}
}

A::a() {}    

The reason is I created a lot of code with methods defined inside the class defintion, without using headers. I do not had cyclic references up to now, but recently there is need to. I don't like to define bunches of methods by the Type::method syntax, as only very few methods have to be known before the latter definition of the class.

So I like somewhat like a backward declaration, declare or define only a few methods before for cyclic references and define the whole class later.


Solution

  • No, there is no way to redefine a class.

    According to the C++ language standard the class definitions is:

    class-specifier:
        class-head { member-specification_opt }
    

    The standard explicitly says that member specification should be complete within class definition:

    Members of a class are data members, member functions (9.3), nested types, and enumerators. The member-specification in a class definition declares the full set of members of the class; no member can be added elsewhere.

    Also the standard gives example of redefinition of class:

    struct S { int a; };
    struct S { int a; }; // error, double definition
    

    is ill-formed because it defines S twice.