473,396 Members | 1,966 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,396 software developers and data experts.

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_ptr 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 1472
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_ptr 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 "housekeeping" 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.com> schrieb im Newsbeitrag
news:11*********************@g14g2000cwa.googlegro ups.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
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...
6
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...
3
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...
3
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. ...
0
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...
1
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...
7
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...
7
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...
9
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 ...
20
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.