473,791 Members | 2,985 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Class A contains class B, class B points to class A


Okay.

So I have a class A, which contains a vector of B.
Any B item is owned by a single, unique A. Indeed, when we create A, A
creates its B items.

B would like to know which A it is contained in, without having to be
perpetually told it by being passed A when B methods are called.
How can B store its A (or some handle to A) if A stores B?

Say that B has a pointer to its A.
However, if I create a copy of an A object and delete the original, all
the B->A pointers are invalidated. Doh!

Another solution would be to have a boost::shared_p tr to A in each B.
But that doesn't work, because when you construct an item of type A and
it wants to create the items of type B, A is a plain object and hasn't
been created using the smart_ptr 'new'.
[sorry to get slightly off-topic from vanilla C++ here]

Anybody have any suggestions?

Maybe someone can suggest an appropriate pattern or way to redesign?

Thanks

Joseph

Dec 30 '05 #1
5 1487
Joseph Turian wrote:
Okay.

So I have a class A, which contains a vector of B.
Any B item is owned by a single, unique A. Indeed, when we create A, A
creates its B items.

B would like to know which A it is contained in, without having to be
perpetually told it by being passed A when B methods are called.
How can B store its A (or some handle to A) if A stores B?
Why specifically does B need to know which A it is contained in? In
fact, why does B need to know about A at all? If a routine retrieves a
B object through A than it will already know the A object with which a
particular B object is associated.
Say that B has a pointer to its A.
However, if I create a copy of an A object and delete the original, all
the B->A pointers are invalidated. Doh!
All the more reason for B not be burdened with knowledge of A. At any
rate the copy semantics of A have to be worked out on their own. And
the more simple the data structures involved, the better.
Another solution would be to have a boost::shared_p tr to A in each B.
But that doesn't work, because when you construct an item of type A and
it wants to create the items of type B, A is a plain object and hasn't
been created using the smart_ptr 'new'.


As long as the A object is allocated dynamically then it can be
maintained by a shared_ptr. But of course the overhead of every B
object in the vector pointing to the same A object significantly
increases memory usage and "housekeepi ng" chores - providing yet
another reason for B not to know anything about A.

The tendency when initially designing data structures is to overdo the
cross references. The thinking is usually that such links are "useful"
or "necessary. " However in practice such links rarely prove their value
and are generally used to replace more thoughtful planning or a more
well-considered design.

A better approach is to start with structures that contain no more than
the bare minimum of information (that is, with no duplicated
information). Less is more. It is always possible to add additional
fields subsequently should such information turn out to be needed.

Greg

Dec 30 '05 #2

Greg wrote:
Why specifically does B need to know which A it is contained in? In
fact, why does B need to know about A at all? If a routine retrieves a
B object through A than it will already know the A object with which a
particular B object is associated.


Because we a typically working with items of type B.

As you suggested, I've rethought the design.

Here's how it used to be:
A inherited from class C. C was the information shared among all the B
items.

Here's how it is now:
A creates a smart ptr to C, which the B also point to.

I think that suffices.

Thanks!

Joseph

Dec 30 '05 #3
hmm

Dec 30 '05 #4
"Joseph Turian" <tu****@gmail.c om> schrieb im Newsbeitrag
news:11******** *************@g 14g2000cwa.goog legroups.com...

Okay.

So I have a class A, which contains a vector of B.
Any B item is owned by a single, unique A. Indeed, when we create A, A
creates its B items.

B would like to know which A it is contained in, without having to be
perpetually told it by being passed A when B methods are called.
How can B store its A (or some handle to A) if A stores B?

Say that B has a pointer to its A.
However, if I create a copy of an A object and delete the original, all
the B->A pointers are invalidated. Doh!


If an object knows that it is owned by another object you shouldn't delete
it without telling its owner. How would you like it, if the money you own is
spent by someone else without telling you?

Also, if an owned object is copied and the new object should also be owned,
the owner of the new object must learn about the existance of the new
object. Otherwise you must not copy the pointer (or whatever) to the owner.

If your objects are so closely related, the owning object must be notified
of all relevant changes of an owned object, and of cause the owning object
must also inform its owned objects of all relevant changes.

It might be usefull in some cases, that an owned object knows about its
owner. But even then an owned object should not implement operations, which
create or delete owned objects. You could provide some method to get the
owner of an object and then call methods of the owning object to create or
remove owned objects. You can even provide such methods for owned objects,
but such methods should only call methods of the owning objects to perform
such tasks.

HTH
Heinz
Dec 30 '05 #5
AD
"Say that B has a pointer to its A.
However, if I create a copy of an A object and delete the original, all
the B->A pointers are invalidated. Doh! "

I think this can be easily taken care by defining appropriate copy
constructor and assignment operator so that when a copy of A is made
then the copy gets its own copy of B and deleting original A won't
leave any dangling pointers in copy.

Dec 30 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
9082
by: Steven T. Hatton | last post by:
The following may strike many of you as just plain silly, but it represents the kind of delelima I find myself in when trying to make a design decision. This really is a toy project written for the purpose of learning to work with C++. It therefore makes some sense for me to give the situation the amount of consideration presented below. To be quite honest, I'm amazed at the amount there is to say about such a seemingly simple...
6
2375
by: surrealtrauma | last post by:
i have a trouble about that: i want to ask user to enter the employee data (employee no., name, worked hour, etc.), but i dont know how to sort the data related to a particular employee as a group. i want to use a array object in the class but i don't know how..i am just learning the c++. So i dont know how to use class. in fact, i have writen like the following: class employee { public: employee();
3
4470
by: David Komanek | last post by:
Hi all, I am trying to learn more about how to use g++/Cygwin to produce dll files on WinXP. And I have a problem which at the first look seems to be an obvious dll-export problem, but I don't think this is really the issue. Here is a description of my problem. MathLibrary.dll: - SpaceTimeVector (uses various stuff from this dll)
3
26502
by: Noozer | last post by:
I have several tags on a webpage of the same class. If the user clicks a specific checkbox I'd like to be able to alter the display property of the class, affecting all objects of that class. This is an intranet application so we know that javascript will be enabled and the browser will be IE. How can I affect all the members of this class? Is there a way I can toggle the DISPLAY property of a class so all the elements using that class...
0
983
by: AnkitAsDeveloper [Ankit] | last post by:
As all we know, in order to remove cyclic includes in C++ we seperate the declarations and definitions of classs and it's member in two files Header (*.h) and source files (*.cpp). This is not a problem for C# as there is no concept of include. I faced problems for seperating declarations and definitions for class when it contains properties. This can also be done in context of struct/structures. Here is how we can. Class A
1
1179
by: AnkitAsDeveloper [Ankit] | last post by:
As all we know, in order to remove cyclic includes in C++ we seperate the declarations and definitions of classs and it's member in two files Header (*.h) and source files (*.cpp). This is not a problem for C# as there is no concept of include. I faced problems for seperating declarations and definitions for class when it contains properties. This can also be done in context of struct/structures. Here is how we can. Class A
7
3466
by: mathieu | last post by:
Hello, I did read the FAQ on template(*), since I could not find an answer to my current issue I am posting here. I have tried to summarize my issue in the following code (**). Basically I am trying to hide the `complexity` of template from the user interface. If you look at the code DataSet should be the object that my user manipulate. Unfortunately by doing so the object returned by DataSet::Get is a FloatingPt, so without the virtual...
7
354
by: triplejump24 | last post by:
Hello. Im working on what seems to me a very complicated programming assignment. I basically have to design a video library using class. The program needs to read data files i have and put the into in arrays. I have to be able ot check out a video, check in a video, get video info, get customer info. Theres alot of steps but i dont need to do all of them to get the points i need. If anyone has anytime to help me i would really really...
9
2031
by: Will | last post by:
I have given up looking for a solution so I figured I would break down and ask. I am trying to modify a CSS class in page_load. I have no problem doing it with an ID with the simple HTMLID.Style = "10px"; however I need to apply it to a CSS class not a single ID. What is the code for that?
20
4044
by: tshad | last post by:
Using VS 2003, I am trying to take a class that I created to create new variable types to handle nulls and track changes to standard variable types. This is for use with database variables. This tells me if a variable has changed, give me the original and current value, and whether the current value and original value is/was null or not. This one works fine but is recreating the same methods over and over for each variable type. ...
0
9669
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9515
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10207
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10155
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9029
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6776
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5559
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4110
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3718
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.