Nested class structure question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • monogeon
    New Member
    • Jan 2022
    • 1

    Nested class structure question

    Hello i'm trying to make my own library. im using qt creator. I've searched online and the best way to do one would be to use nested classes. The problem is the code below works fine in single file, but i'd like to seperate it into different files

    example code:

    Code:
    class A
    {
    public:
        class B
        {
        public:
            class C
            {
            public:
            };
            class D
            {
            public:
            };
        };
    };
    To do that i'd want to make seperate .h and .cpp files for eg every class.

    Code:
    //headerA
    #include "B.hpp"
    class A
    {
    public:
        class B;
    };
    
    //headerB
    #include "A.hpp
    #include "C.hpp"
    #include "D.hpp"
    class A::B
    {
    public:
        class C;
        class D;
    };
    
    //headerC
    #include "B.hpp"
    class A::B::C
    {
    public:
    };
    
    //headerD
    #include "B.hpp"
    class A::B::D
    {
    public:
    };
    thus my class A header needs include header for B. but to connect B to A, in class B i need to do A::B which requires me to include header A as well. So in short both files need to include each other, but that in tern gives an error. Is there a way around this? or maybe im doing it all wrong. Thank you for the help in advance. If any more info is needed please ask away.
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    What you are suggesting will not work, if you really want to use nested classes then your only real option for declaration is the first file you gave

    Code:
    class A
    {
    public:
        class B
        {
        public:
            class C
            {
            public:
            };
            class D
            {
            public:
            };
        };
    };
    I would not do this as you header will be horrendous to read. I would declare the classes completely independently, i.e. A, B, C and D declared in there own separate heads and source files only referencing the other classes in member attributes and method parameters and return values.

    Without more detail it is not clear what problem you think this nested structure will solve so it is hard to know precisely what to suggest.

    Comment

    • wesleygunter
      New Member
      • Feb 2022
      • 1

      #3
      Thank you for the recommendation. I'll be sure to check it out.

      Comment

      Working...