473,776 Members | 1,513 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

specialized containment???

Hello, maybe somebody can enlighten me? :)
I wasn't really sure how to concisely describe the following scenario
so it was tricky to search for help.
Simplifying the problem...
I have a home which contains people. I also want a SpecialHome which
has to contain a set of SpecialPersons.
class Home {
Person* getPerson(id);

vector<Person*> m_People;
}

class SpecialHome : public CHome {
SpecialPerson* getPerson(id);
vector<SpecialP erson*> m_People;
}
So obviously the above SpecialHome class isn't how I would want to
implement this. There's nothing being inherited, the list of pointers
are unnecessarily duplicated, etc. The problem is that a number of
Home methods accept and return Persons. In my more complicated
situation, I really want to avoid a templatized Home class (it would
make for quite a confusing class diagram). I guess accepting Persons
isn't such a problem thanks to polymorphism, but returning them is
(I've also got a Person iterator class in an effort to abstract away
the containment mechanism in Home).

The approach I've tried taking is to remove all of these Person-related
methods from Home and put them is a sort of templatized "manipulato r"
class. This manipulator was made a friend of Home and SpecialHome so
it more or less can do whatever it pleases. Naturally, there's a lot
of dynamic_casting since the vector lives in the base class (for some
reason dynamic_casting always makes me uncomfortable).

I'm having trouble getting this to compile because of complications
with the more convoluted scenario I'm dealing with.
This lead me to take a step back and ask myself if there isn't a better
way to do design the class structure. I would imagine the scenario I
described above is a fairly common one.

Any tips are greatly appreciated!
cheers!

Nov 22 '05 #1
3 1350
* Mr Dyl:

I have a home which contains people. I also want a SpecialHome which
has to contain a set of SpecialPersons.
class Home {
Person* getPerson(id);

vector<Person*> m_People;
}

class SpecialHome : public CHome {
SpecialPerson* getPerson(id);
vector<SpecialP erson*> m_People;
}


That sketch isn't really enough to go on, because you haven't indicated
access, and for this kind of problem what's private and what's public
and what's const or not is key.

Here's a more fleshed-out sketch that illustrates the fundamental
problem you're up against:

class Person {};
class ShortPerson: public Person {};
class LongPerson: public Person {};

class Home
{
public:
addPerson( Person* );
std::vector<Per son const*> persons() const;
};

class ShortPersonHome : public Home
public:
addPerson( ShortPerson* );
std::vector<Sho rtPerson const*> persons() const;
};

class LongPersonHome: public Home
public:
addPerson( LongPerson* );
std::vector<Lon gPerson const*> persons() const;
};

int main()
{
ShortPersonHome home;
Home* pHome = &home;
LongPerson longPerson;

pHome->addPerson( &longPerson ); // Oops, roof is too low!
}

As you can see it's a problem of mutability, not of downcasting the
internal representation (although that's also a problem, but managable).

What you need is something like

// Home
// MutableHome
// ShortPersonHome
// MutableShortPer sonHome
// LongPersonHome
// MutableLongPers onHome

class Home
{
public:
std::vector<Per son const*> persons() const;
};

class MutableHome: public Home
{
public:
addPerson( Person* );
};

class ShortPersonHome : public Home
public:
std::vector<Sho rtPerson const*> persons() const;
};

class MutableShortPer sonHome: public ShortPersonHome
{
public:
addPerson( ShortPerson* );
};

class LongPersonHome: public Home
{
public:
std::vector<Lon gPerson const*> persons() const;
};

class MutableLongPers onHome: public LongPersonHome
{
public:
addPerson( LongPerson* );
};

Now in ShortPersonHome you can safely assume that any Person in there is
really a ShortPerson, because no other kinds of persons can be added.
So you can simply provide an internal accessor function that downcasts.
As I wrote, that part is a managable problem, also with other solutions:
note that C++ does support covariance for function results.

Hth.,

- Alf

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 22 '05 #2
Hey Alf,
thanks for the reply.
Interesting. At the moment I'd only been thinking about the
relationship between parent and child classes so I hadn't even been
thinking about the problem you pointed out.

A question though,

Home contains (I assume) a list of persons accessed through:

std::vector<Per son const*> persons() const

LongPersonHome also provides access to this list (but they are
LongPersons now):

std::vector<Lon gPerson const*> persons() const;

How are you able to cast a std::vector<Per son const*> to
std::vector<Lon gPerson const*>?
I think maybe there's something fundamental here that I'm not seeing.
Thanks again!
Dylan

Nov 22 '05 #3
* Mr Dyl:

Home contains (I assume) a list of persons accessed through:

std::vector<Per son const*> persons() const

LongPersonHome also provides access to this list (but they are
LongPersons now):

std::vector<Lon gPerson const*> persons() const;

How are you able to cast a std::vector<Per son const*> to
std::vector<Lon gPerson const*>?
Note that the signature of 'persons' specifies a copy.

A mutable collection of T isn't really convertible along with the type
of T. That's a problem with Java arrays, for example, where Java is
reduced to run-time type-checking, throwing an exception if the type of
an element assigned to an array is of the wrong dynamic type,
<url:
http://java.sun.com/docs/books/jls/third_edition/html/arrays.html#10. 10>,
which implies a run-time check of every assignment to a Java array.

Note that that this concerns an array casted analogous to casting
std::vector<Lon gPerson const*> to std::vector<Per son const*>, an upcast,
which is opposite and many might think safer than the cast you mention.

So, let 'persons' create a _copy_... ;-)

Or, its return type is non-mutable collection, of a collection type that
supports this kind of thing; that means essentially, a collection type
you have defined.

Or, represent it by a collection of member functions that provide
iteration.

Or, represent it by a general standard-library compatible iterator.

I think maybe there's something fundamental here that I'm not seeing.


Yes, you're now up against the idea of "virtual data".

<url:
http://www.parashift.c om/c++-faq-lite/value-vs-ref-semantics.html# faq-31.2>.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 22 '05 #4

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

Similar topics

2
4515
by: Chandrashekara Adiga | last post by:
hi This is particular to Containment classes .. ( just taking an example from rougewave ) Can someone point out How destrutor of new RWDate(2, "Nov", 1980) will be called? Is is our responsibilty to call it or such container after deleting the list element calls the destuctor also .
0
933
by: Jim Douglas | last post by:
{System.Collections.Specialized.ListDictionary.NodeKeyValueCollection} : {System.Collections.Specialized.ListDictionary.NodeKeyValueCollection} Count: 25 ?entry.Contains("MachineName") true
1
3930
by: Dave | last post by:
Hello all, The methodology of policy-based design states that one should inherit from policy classes and, of course, these classes must provide an agreed-upon public interface and semantics. I would like to understand better why inheritance, rather than containment, is espoused. The only things I can see that inheritance provides beyond containment is the possibility of polymorphism and access to protected members. Since neither of...
3
1688
by: Ruben Campos | last post by:
I have a template class, and I want to create partially (totally) specialized versions of it, by adding new methods to the original base template specification. But it seems that partially (totally) specialized templates are completely independent from their base templates, so in order to do what I want, I have to redeclare and redefine (in both cases duplicating code) all methods and attributes of the base template in each of the...
1
3250
by: Baski | last post by:
Does anyone knows any article explains effective use of containment and aggregation with interfaces using C#. Thanks Baski
4
7821
by: Kerr | last post by:
Hi all, I've been scouring the internet for help with this problem and every occurance i've seen reconstructs the problem but no one seems to have a solution. Hoping that you guys can help me. I have a vb.net windows forms project that is using a app.config file. the contents of the app.config is staggered. see a snippet below: <?xml version="1.0" encoding="utf-8" ?> <configuration>
0
1891
by: David Veeneman | last post by:
This post is for the Google crawler-- no response in required. Can a containment hierarchy made up of two collections derived from BindingList<T> be data-bound to master-detail dataGridView controls? Assume a collection, CustomerList, derived from BindingList<T>, with a property Orders, of type OrderList, also derived from BindingList<T>. Here is how to create a master-detail pair of grids: In design mode, create a form with two...
0
2463
by: passion | last post by:
"Specialized Search Engines" along with Google Search Capability (2 in 1): http://specialized-search-engines.blogspot.com/ Billions of websites are available on the web and plenty of extremely good search engines are there like Google, Yahoo and Live to name few of them. Though this search engines have extremely efficient, complex and beautiful algorithms designed by gems of the industry, but still they may not deliver best results for...
12
5471
by: Daz | last post by:
Hi guys, I'm trying to make a script.aculo.us sortable list have a limited number of elements in it. Now I can check for this really easily and have a warning message, and of course the php throws an error back anyway so they can't actually save more than the limit, but, is there a way I can stop them from dropping the damn thing in the list if it's full? I thought about dynamically setting the containment array, so that
0
9462
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
9922
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8951
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...
1
7469
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5367
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5492
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4030
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
3621
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2859
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.