469,907 Members | 2,184 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,907 developers. It's quick & easy.

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:

Expand|Select|Wrap|Line Numbers
  1. class A
  2. {
  3. public:
  4.     class B
  5.     {
  6.     public:
  7.         class C
  8.         {
  9.         public:
  10.         };
  11.         class D
  12.         {
  13.         public:
  14.         };
  15.     };
  16. };
To do that i'd want to make seperate .h and .cpp files for eg every class.

Expand|Select|Wrap|Line Numbers
  1. //headerA
  2. #include "B.hpp"
  3. class A
  4. {
  5. public:
  6.     class B;
  7. };
  8.  
  9. //headerB
  10. #include "A.hpp
  11. #include "C.hpp"
  12. #include "D.hpp"
  13. class A::B
  14. {
  15. public:
  16.     class C;
  17.     class D;
  18. };
  19.  
  20. //headerC
  21. #include "B.hpp"
  22. class A::B::C
  23. {
  24. public:
  25. };
  26.  
  27. //headerD
  28. #include "B.hpp"
  29. class A::B::D
  30. {
  31. public:
  32. };
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.
1 Week Ago #1
1 3841
Banfa
9,065 Expert Mod 8TB
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

Expand|Select|Wrap|Line Numbers
  1. class A
  2. {
  3. public:
  4.     class B
  5.     {
  6.     public:
  7.         class C
  8.         {
  9.         public:
  10.         };
  11.         class D
  12.         {
  13.         public:
  14.         };
  15.     };
  16. };
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.
1 Week Ago #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by Stephane Ninin | last post: by
9 posts views Thread by OKB (not okblacke) | last post: by
4 posts views Thread by serge calderara | last post: by
4 posts views Thread by KInd | last post: by
reply views Thread by MWK | last post: by
9 posts views Thread by Sid Price | last post: by
1 post views Thread by =?Utf-8?B?QU5lZWxpbWE=?= | last post: by
1 post views Thread by Waqarahmed | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.