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

Inheritence/substitutability dilemma

424 256MB
I have two unrelated types of data elements (objects) which I want to hold in two related types of containers, one for each element type. But this seems contradictory - for consistency, it seems that either
  1. the containers should be unrelated through inheritance, or
  2. the elements should be related through inheritance.
The problem with (a) is that I'd like to specify a base class interface for the containers which necessarily relates them through inheritance, and the problem with (b) is that the elements are have very different methods and members so defining a base class for those elements to shoehorn them into the container base class interface would be kind of a hack.

In other words, my elements do not follow the principle of substitutability of objects that are related through inheritence, but their containers do, apart from a single method addelement() which adds the disparate elements to the containers.

What would be a good solution to this dilemma? Has anyone come across such a situation before?

(I originally posted this question as part of a larger question in this thread which which did not attract many responses, so I'm splitting up my question into different parts and skipping implementation details as suggested there.)
Jun 25 '08 #1
4 1393
weaknessforcats
9,208 Expert Mod 8TB
I suggest your containers contain objects of class T. That is, use a template for the containers.

Now the objects that are contained have very different interfaces so you can't use a base class for these interfaces. Instead, implement a Visitor on the container and get at your methods for the T objects using a Visitor object for that T.

There is an article in the C/C++ Articles forum on the Visitor design pattern. In that article are code samples that implement exactly what your are looking for.
Jun 25 '08 #2
arnaudk
424 256MB
Thanks for the reply, w4cats. I have a look at that article again.
Jun 25 '08 #3
RRick
463 Expert 256MB
From your post, the 2 different objects are stored in two different containers. As W4cats suggested, templates are a good way to define the containers. They will hold the correct type of object and make sure you don't mix and match them incorrectly.

Now you have two containers that are related. Okay, make a class that has both of the containers as members. In this case, the parent class has the two containers as children (i.e. is-a relation). Now, the parent class has access both containers and the parent public API can define what needs to be done to both containers.
Jun 26 '08 #4
arnaudk
424 256MB
Hi, RRick, and thanks for your reply!
make a class that has both of the containers as members. ... (i.e.
is-a relation).
That would appear to be more of a has-a relationship to me. The parent class here does not serve as an interface to its children in a way that I can use polymorphic base class pointers. Indeed, I have two types of containers, but more than two containers. So using a parent class means I'll have to know in advance how many containers there will be, unless I use a composite, is that what you're alluding to? Even then I'd still need a (more complicated) base class for the elements and containers...
Jun 26 '08 #5

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

Similar topics

1
by: John | last post by:
Hi, I am trying to create a class heirarchy similar to the following: // base interface class ICar { public: virtual void start() = 0; }; // add members to that interface, but retain base...
8
by: Digital Puer | last post by:
I made the following table to help me (re)learn inheritence basics. Can someone check if it's correct? This table requires a courier-like font. Java C++ ---- ...
5
by: john bailo | last post by:
For a c# web application, I created a user control that includes a form in control. The idea is, on the main Page, when the user clicks Submit from the master form, that makes the user control...
0
by: Jan Elbęk | last post by:
Hi, I would like to make a base form in my project - which (almost) all forms must inherit from. The baseform must have some visible elements (a toolbar, a topaligned panel and a picturebox and...
16
by: gorda | last post by:
Hello, I am playing around with operator overloading and inheritence, specifically overloading the + operator in the base class and its derived class. The structure is simple: the base class...
19
by: JKop | last post by:
Been thinking about the following: class Mammal { public: virtual void Mate(Mammal &) = 0; };
7
by: preetam | last post by:
Hi, This question is more towards design than towards c++ details. By looking at books on design patterns and various google threads on the same topic, I see that composition is favoured to...
5
by: Neelesh Bodas | last post by:
This might be slightly off-topic. Many books on C++ consider multiple inheritence as an "advanced" concept. Bruce Eckel says in TICPP, volume 2 that "there was (and still is) a lot of...
16
by: hzmonte | last post by:
Correct me if I am wrong, declaring formal parameters of functions as const, if they should not be/is not changed, has 2 benefits; 1. It tells the program that calls this function that the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...

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.