473,287 Members | 1,426 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 software developers and data experts.

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.
Jan 7 '22 #1
2 14414
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.
Jan 12 '22 #2
wesleygunter
1 Bit
Thank you for the recommendation. I'll be sure to check it out.
Feb 22 '22 #3

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

Similar topics

1
by: Stephane Ninin | last post by:
Hello all, I am trying to play with nested class in a script I am making, and I am not sure I really understand how they work. Here is some code: __all__ =
9
by: OKB (not okblacke) | last post by:
For a variety of reasons, I'm interested in putting together some code that will allow me to created structures out of nested classes, something like: class class1: def methA(self): print...
4
by: serge calderara | last post by:
Dear all I have an Object that I will call for instance VEHICULE. This object contains some other type of objects like CAR and BICYCLE VEHICULE has necessary at least one object of type CAR or...
4
by: KInd | last post by:
Hello All, When is nested class more preferable that Inheritance ? I think with proper inheritance and friend class concept we can get the same flexibility as nested classes Any comments .. Best...
0
by: MWK | last post by:
Hi All, I use VS2003 and managed code to compile a DLL. When I use a nested class, the event always cause an error: LINK : error LNK2020: unresolved token (06000004)...
3
by: Tony Young | last post by:
Hi, I am very interested to know which one of the two functions below is used more often by people. The 1st way intends to let B prepare the data and A perform the action. The 2nd way intends...
9
by: Sid Price | last post by:
Hello, I have a problem trying to figure out the following design issue. I have a base class and a number of classes derived from that base. A method in the base class covers 90% of the...
4
by: rach | last post by:
I just started to learn C++. I copied the following code from a data structure textbook to a ".h" file and couldn't compile it. The code contains three template interfaces. One inherits another. The...
1
by: =?Utf-8?B?QU5lZWxpbWE=?= | last post by:
I am using C# and I created an .aspx page and the corresponding .aspx.cs code behind file. In the code behind file I have the following class structure public partial class myclass : page { ...
2
by: K Viltersten | last post by:
Suppose there is the following class structure. class S {...} class A : S {...} class B : S {...} class A1 : A {void m(){...}} class B1 : B {void m(){...}} class B2 : B {...} Just to clarify...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.