c++ - How to create a class setup where two classes each holds an instance of the other? - Stack Overflow
I have a class setup like this, where each class holds a member of the other one:
class b;
class a
{
b var;
};
class b
{
a var;
};
How do I make it work?
I have a class setup like this, where each class holds a member of the other one:
class b;
class a
{
b var;
};
class b
{
a var;
};
How do I make it work?
Share Improve this question edited yesterday wohlstad 26.8k16 gold badges54 silver badges83 bronze badges asked yesterday SvyatSvyat 355 bronze badges 6 | Show 1 more comment2 Answers
Reset to default -1This setup, as it's written, isn't possible - an instance of a
contains an instance of b
, which in turn contains an instance of a
, which then contains an instance of b
, ad infinitum.
Instead, you need the classes to hold pointers to each other:
class a {
b* b;
};
class b {
a* a;
};
At least one of your var
s should be a [smart] pointer, to avoid an infinite inclusion (a
holds b
which holds a
which holds b
etc...).
This enables to use a forward declaration and break the inclusion cycle.
The decision if one is enough is dependant on the nature of your entities.
The relevant cases:
If both entities are separate and should only be be referring to each other, both should use a pointer:
class Entity1; // forward declaration class Entity2 { Entity1 * pEntity1; } class Entity2; // forward declaration (required if Entity1 is defined in a separate module) class Entity1 { Entity2 * pEntity2; };
If one entity can be considered as a parent of the other (the child), the parent can (but is not required to) hold the child "by value" and the only the child should use a pointer:
class Parent; // forward declaration class Child { Parent * pParent; }; class Parent { Child child; };
Note that using 2 pointers is always a valid option, even in the case of parent/child.
A side note:
The pointers above might be replaced by smart pointers.
But this requires to address another issue of lifetime and ownership which I prefered to leave out of this answer to limit it to the scope of the question.
- 50公里的传奇 Ubiquiti Networks
- [连载]巨头“心血之作”终失败(五):惠普TouchPad
- 有事请烧纸!清明祭奠已死的平板电脑
- Integration-testing a Spring Boot @Service without SpringBootApplication - Stack Overflow
- javascript - Submit in .jsp file not 'seeing' dijit.byid tabs in associated Dojo.js file - Stack Overflow
- c++ - OpenCV Build Fails with "FilesNVIDIA.obj" Error in CMake and Visual Studio - Stack Overflow
- azure active directory - Entra External Id: Include values provided by token requester in access token claims - Stack Overflow
- javascript - How to create perfect hash with ASCII symbols as input, where output hash is always the same for each ASCII sequenc
- Swift - Calendar Ignores Locale - Stack Overflow
- javascript - typeorm trying to delete relation table data when there is no change - Stack Overflow
- gdb - How to properly load and pass a file path to open syscall in x86_64 assembly? - Stack Overflow
- swift - SwiftUI ScrollView not scrolling with DragGesture inside a ForEach Item - Stack Overflow
- caching - How is the userUX affected by cache purge by a WordPress plugin? Cookie consent settings is not affected? - Stack Over
- rust - Remove struct from vector while mutably iterating through it - Stack Overflow
- javascript - Why does the iterator close after a break in a for...of loop? - Stack Overflow
- ZeroMQ Subscriber in Rails Worker Fails to Receive Messages, but Works with QLStats - Stack Overflow
- html - CSS "vertical-align: middle" doesn't shrink the height of the parent element of img - Stack Ove
var
s should be a [smart] pointer toa
/b
. – wohlstad Commented yesterdaysizeof(a)
is ∞, which is pretty big. Likewise,sizeof(b)
is also ∞. To instantiate one of these objects, you may need a RAM upgrade. – Eljay Commented yesterday