C++ Forward Declaring Classes Within Classes

villapx

The following simple piece of code compiles, although I don't understand why:

class C {
    class B;

    class A {
        B getB() { return B(); }
    };

    class B {
    };
};

int main(int, char**)
{
    return 0;
}

If I then comment out the "class C" stuff, so that the forward declaration of B, the definition of A and the definition of B are no longer nested within a class, the code does not compile, since B is of an incomplete type:

main.cpp: In member function 'B A::getB()':
main.cpp:6: error: return type 'struct B' is incomplete
main.cpp:6: error: invalid use of incomplete type 'struct B'
main.cpp:3: error: forward declaration of 'struct B'

I understand what it means for a type to be incomplete, namely that it has not been defined yet and so the compiler can't possibly know how much space to allocate for it. But why is B not considered incomplete in the code above, where A and B are both declared and defined inside of C?

Barry

I believe this is a consequence of [basic.scope.class]:

The potential scope of a name declared in a class consists not only of the declarative region following the name’s point of declaration, but also of all function bodies, default arguments, exception-specifications, and brace-or-equal-initializers of non-static data members in that class (including such things in nested classes).

That is, the scope of the full declaration of B includes the body of the member function of the nested class:

class C {
    class B; // (1)

    class A { 
        B getB() {
            return B(); // both (1) and (2) in scope here
                        // since (2) is the complete type declaration,
                        // this is perfectly fine
        }
    };

    class B { // (2)
    };
};

By comparison, if C were a namespace instead of a class, the scope of the full declaration of class B would not extend into A::getB(). The only visible declaration would be the forward-declaration of B that I labeled (1) - so B() would be the construction of an incomplete type there.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Python Classes Within a Class

分類Dev

Declaring movements to symbols from other classes

分類Dev

C# Array of Classes

分類Dev

Error In classes C++

分類Dev

Resolving Diamond Inheritance within Python Classes

分類Dev

AspectJ - exclude inner classes from within clause

分類Dev

C: Forward declaring a typedef that will be defined later for being used in declaring a function now

分類Dev

Will forward-declaration of two classes lead to circular dependency in the constructor?

分類Dev

Base and derived classes C++

分類Dev

C++ Array of Derived Classes

分類Dev

C++ and incomplete friend classes

分類Dev

Forward declaring and using in one step

分類Dev

C++: Passing classes as argument to methods of other classes

分類Dev

How to compile a nodejs Typescript project with classes in separate files within NTVS?

分類Dev

What is the portable way to forward-declare classes in an inline namespace in libc++?

分類Dev

OOPs in C. Default values for classes

分類Dev

Template Classes in C++ and Function Overloading

分類Dev

Deserialize to existing instance of classes in C#

分類Dev

Is there a way to REQUIRE derived classes be sealed in C#?

分類Dev

Data modification between classes C#

分類Dev

C# Unit Test is not recognizing other classes

分類Dev

Python C api iterate through classes in module

分類Dev

Using a struct across classes in c++

分類Dev

c# multiple classes same methods

分類Dev

C++ equivalent of C# nested private classes

分類Dev

Using TailwindCSS and the Typography plugin, how do I allow for customizations within .prose using classes?

分類Dev

Converting ArrayList to HashMap, however, selecting choice objects with different variable classes within ArrayList

分類Dev

Unable to use helper classes within unit tests of a bundled aurelia app. RequireJS Configuration?

分類Dev

Declaring C# properties

Related 関連記事

  1. 1

    Python Classes Within a Class

  2. 2

    Declaring movements to symbols from other classes

  3. 3

    C# Array of Classes

  4. 4

    Error In classes C++

  5. 5

    Resolving Diamond Inheritance within Python Classes

  6. 6

    AspectJ - exclude inner classes from within clause

  7. 7

    C: Forward declaring a typedef that will be defined later for being used in declaring a function now

  8. 8

    Will forward-declaration of two classes lead to circular dependency in the constructor?

  9. 9

    Base and derived classes C++

  10. 10

    C++ Array of Derived Classes

  11. 11

    C++ and incomplete friend classes

  12. 12

    Forward declaring and using in one step

  13. 13

    C++: Passing classes as argument to methods of other classes

  14. 14

    How to compile a nodejs Typescript project with classes in separate files within NTVS?

  15. 15

    What is the portable way to forward-declare classes in an inline namespace in libc++?

  16. 16

    OOPs in C. Default values for classes

  17. 17

    Template Classes in C++ and Function Overloading

  18. 18

    Deserialize to existing instance of classes in C#

  19. 19

    Is there a way to REQUIRE derived classes be sealed in C#?

  20. 20

    Data modification between classes C#

  21. 21

    C# Unit Test is not recognizing other classes

  22. 22

    Python C api iterate through classes in module

  23. 23

    Using a struct across classes in c++

  24. 24

    c# multiple classes same methods

  25. 25

    C++ equivalent of C# nested private classes

  26. 26

    Using TailwindCSS and the Typography plugin, how do I allow for customizations within .prose using classes?

  27. 27

    Converting ArrayList to HashMap, however, selecting choice objects with different variable classes within ArrayList

  28. 28

    Unable to use helper classes within unit tests of a bundled aurelia app. RequireJS Configuration?

  29. 29

    Declaring C# properties

ホットタグ

アーカイブ