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

Subset of derived classes accessing base class functions

Say I have a base class B and four derived classes d1, d2, d3, d4. I
have three functions fx, fy, fz such that:
fx should only be called by d1, d2
fy should only be called by d2, d3
fz should only be called by d1, d3, d4

I think I have two options.
(1) Make all functions virtual and define them in the required derived
classes. This will of course lead to a lot of code duplication and
problems in maintainability.
(2) Keep the functions in the base class. Define some type of
identifier (ex: a string) for each of the derived classes and add some
checks at the top of each fx, fy, fz to ensure that only the
appropriate di's are accessing them.

I have implemented (2) but I'm not happy with it because I don't seem
to use the identifier for anything else. Is it possible to use
something like typeid instead for the same purpose?

Is there any other alternate way to do something like this? May be a
design pattern that can be adapted to handle such a situation?
Jul 24 '08 #1
9 1931

fgh.vbn....@gmail.com wrote:
Say I have a base class B and four derived classes d1, d2, d3, d4. I
have three functions fx, fy, fz such that:
fx should only be called by d1, d2
fy should only be called by d2, d3
fz should only be called by d1, d3, d4
Is there any other alternate way to do something like this? May be a
design pattern that can be adapted to handle such a situation?
Maybe inserting another hierarchy level an multiple hierarchy:

class B{
//all common thing
virtual ~B();
}

class FXToImplement: public B
{
virtual void fx();
virtual ~FXToImplement()=0;
}

class FYToImplement: public B
{
virtual void fy();
virtual ~FYToImplement()=0;
}

class FZToImplement: public B
{
virtual void fz();
virtual ~FZToImplement()=0;
}

class d1: public FXToImplement,FZToImplement
{
void fx();
void fz();
}

and you can continue with the others
Jul 24 '08 #2
On Jul 24, 9:08 pm, fgh.vbn....@gmail.com wrote:
Say I have a base class B and four derived classes d1, d2, d3, d4. I
have three functions fx, fy, fz such that:
fx should only be called by d1, d2
fy should only be called by d2, d3
fz should only be called by d1, d3, d4
Then you have a fundamental design problem. Something is wrong
with this scenario. The base class doesn't know about the
derived classes, so it can't impose any restrictions on a subset
of them. And if the restriction comes from the derived class
itself, then the class knows about it, and will conform to it.

[...]
Is there any other alternate way to do something like this?
May be a design pattern that can be adapted to handle such a
situation?
Perhaps you'd best describe the problem you're trying to solve,
and not just the solution which doesn't work.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jul 24 '08 #3

James Kanze wrote:
On Jul 24, 9:08 pm, fgh.vbn....@gmail.com wrote:
Say I have a base class B and four derived classes d1, d2, d3, d4. I
have three functions fx, fy, fz such that:
fx should only be called by d1, d2
fy should only be called by d2, d3
fz should only be called by d1, d3, d4

Then you have a fundamental design problem. Something is wrong
with this scenario. The base class doesn't know about the
derived classes, so it can't impose any restrictions on a subset
of them. And if the restriction comes from the derived class
itself, then the class knows about it, and will conform to it.

[...]
Is there any other alternate way to do something like this?
May be a design pattern that can be adapted to handle such a
situation?

Perhaps you'd best describe the problem you're trying to solve,
and not just the solution which doesn't work.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orient�e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S�mard, 78210 St.-Cyr-l'�cole, France, +33 (0)1 30 23 00 34
Perhaps you are right :-(. Let me write out the problem in better
terms.

Say I have a base class called Car.
I have four different cars Car1, Car2, Car3, Car4.
There are three types of tyres TyreX, TyreY, TyreZ.
TyreX can only "be applied to" Car1, Car2.
TyreY only to Car2, Car3.
TyreZ only to Car1, Car3, Car4.

As the previous poster suggested I could possibly first have derived
classes CarWithTyreX, CarWithTyreY, CarWithTyreZ. And then Car1 is
derived from CarWithTyreX, CarWithTyreZ etc.,

But the problem with that approach is that the Car has many other
features (engine, windows etc.,). And I definitely shouldn't be doing
derived classes like CarWithTyreXEngineE1..... That's why I was
thinking of just deriving Car1, ... Car4 directly from Car and then
applying all other functions appropriately.

Also, note that the features aren't just "building blocks" as such.
They can be just functions that modify the object's properties in some
way.

Is there a mistake in this approach?
Jul 24 '08 #4
Hi,

Darío Griffo schrieb:
class FXToImplement: public B
probably you meant

class FXToImplement: virtual public B

otherwise you have multiple instances of B in your derived class.
Marcel
Jul 24 '08 #5
On Jul 24, 6:33*pm, Marcel Müller <news.5.ma...@spamgourmet.com>
wrote:
Hi,

Darío Griffo schrieb:
class FXToImplement: public B

probably you meant

* *class FXToImplement: virtual public B
absolutely

Jul 24 '08 #6
Hi!

fg*********@gmail.com schrieb:
Perhaps you are right :-(. Let me write out the problem in better
terms.
[...]

you wrote exactly the same except that you changed the class and method
names.

We still don't know where the dependency of some methodes to the derived
classes come from. Why must they no be called for some derived classes?
What happens in that case?

As the previous poster suggested I could possibly first have derived
classes CarWithTyreX, CarWithTyreY, CarWithTyreZ. And then Car1 is
derived from CarWithTyreX, CarWithTyreZ etc.,
But the problem with that approach is that the Car has many other
features (engine, windows etc.,). And I definitely shouldn't be doing
derived classes like CarWithTyreXEngineE1..... That's why I was
thinking of just deriving Car1, ... Car4 directly from Car and then
applying all other functions appropriately.
? - nobody prevents you from implementing the common features in the
virtual baseclass Car.

Also, note that the features aren't just "building blocks" as such.
They can be just functions that modify the object's properties in some
way.
That makes no difference in any way.
Marcel
Jul 24 '08 #7


Marcel Müller wrote:
Hi!

fg*********@gmail.com schrieb:
Perhaps you are right :-(. Let me write out the problem in better
terms.
[...]

you wrote exactly the same except that you changed the class and method
names.

We still don't know where the dependency of some methodes to the derived
classes come from. Why must they no be called for some derived classes?
What happens in that case?

Well, what I was trying to convey was that a certain type of car can
only be fitted with a certain type of Tyre. For instance, Car1 is
compatible only with TyreX and TyreZ. If it attempts to be fitted with
TyreY then we can of course print a warning and/or throw an
exception.

I was wondering if there is a way to entirely prevent Car1 from having
a way to call the TyreY function. Sorry if I'm being unclear.

Thanks.
Jul 24 '08 #8
fg*********@gmail.com schrieb:
>We still don't know where the dependency of some methodes to the derived
classes come from. Why must they no be called for some derived classes?
What happens in that case?

Well, what I was trying to convey was that a certain type of car can
only be fitted with a certain type of Tyre. For instance, Car1 is
compatible only with TyreX and TyreZ.
Of course, but neither your class is a car nor it have tyres. The
explanatory power of the paradigm ends here.
If it attempts to be fitted with
TyreY then we can of course print a warning and/or throw an
exception.
This is no problem. You can easily check this at runtime by overloading
some of your funtions in some of the derived classes with an empty stub
that only throws some not_supported_exception.
I was wondering if there is a way to entirely prevent Car1 from having
a way to call the TyreY function. Sorry if I'm being unclear.
If you want to check for this at compile time, the compatibility matrix
has to be part of your class tree. Dario told you how to implement this.
Marcel
Jul 24 '08 #9
On Jul 24, 10:44 pm, fgh.vbn....@gmail.com wrote:
James Kanze wrote:
On Jul 24, 9:08 pm, fgh.vbn....@gmail.com wrote:
Say I have a base class B and four derived classes d1, d2, d3, d4. I
have three functions fx, fy, fz such that:
fx should only be called by d1, d2
fy should only be called by d2, d3
fz should only be called by d1, d3, d4
Then you have a fundamental design problem. Something is
wrong with this scenario. The base class doesn't know about
the derived classes, so it can't impose any restrictions on
a subset of them. And if the restriction comes from the
derived class itself, then the class knows about it, and
will conform to it.
[...]
Is there any other alternate way to do something like
this? May be a design pattern that can be adapted to
handle such a situation?
Perhaps you'd best describe the problem you're trying to
solve, and not just the solution which doesn't work.

Perhaps you are right :-(. Let me write out the problem in
better terms.
Say I have a base class called Car.
I have four different cars Car1, Car2, Car3, Car4.
There are three types of tyres TyreX, TyreY, TyreZ.
TyreX can only "be applied to" Car1, Car2.
TyreY only to Car2, Car3.
TyreZ only to Car1, Car3, Car4.
The first question is: why won't all of the tire types work with
all of the car types? Since you've used suggestive names from a
domain I vaguely know (I own a car), I can guess: each car has a
certain number of constraints concerning the tires it can use
(size being the most obvious one).

You can solve this either at runtime or through the type system.
Normally, static verification (type errors) are to be preferred
to run-time errors, but think about it for awhile: suppose your
code has a pointer to a Car, and you want to apply some Tire* to
it. Given that you don't know the actual type of either Car or
Tire until runtime, how can the compiler tell whether there is a
mismatch or not.

The usual solution here is to provide a virtual function in Car,
which returns the size, etc. (or a list of the sizes, etc.)
which it can accept for Tire. And of course, a function in Tire
which returns its size, etc. Which means a runtime check. But
again, this more or less corresponds to reality: if you go to buy
tires for your car, you won't go to a tire shop specialized in
selling only tires for that particular make and model of car;
you'll go to a general tire shop, pick out a set of tires, and
then check whether they meet the constraints given in your
owners manual (a runtime check).

Note that there can be exceptions: if you can provide a compile
time check, by all means do so. But this generally means
defining additional interfaces, derived from Car and Tire (e.g.
F1RacingCar, F1RacingTires). At that point, code which knows
it's dealing with F1RacingCars and F1RacingTires can get the
compile time check, and avoid the runtime one. (But such code
won't use the basic Car/Tire interface, but rather the derived
interface.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jul 25 '08 #10

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

Similar topics

2
by: Luca | last post by:
Hi, I have a quite complex question to ask you: I have defined a base class where I would like to have a map holding pointers to member functions defined in derived classes. To be more precise...
24
by: Shao Zhang | last post by:
Hi, I am not sure if the virtual keyword for the derived classes are required given that the base class already declares it virtual. class A { public: virtual ~A();
3
by: Bangalore | last post by:
Hi, In the following program, eventhogh two member function declared under private section of the derived class are accessable by derived class pointer. Please clarify me how can derived class...
2
by: Jessica | last post by:
I have a base class and a derived class, but I am getting errors when I try to access functions of the derived class. Simplified version of my code is as follows: //////////////// // test2.hh...
6
by: ivan.leben | last post by:
I want to write a Mesh class using half-edges. This class uses three other classes: Vertex, HalfEdge and Face. These classes should be linked properly in the process of building up the mesh by...
3
by: keith.thornhill | last post by:
hey all, got a problem here using Visual basic .net 2005 i have two pairs of base/derived classes. lets call them Base/Derived and BaseStruct/DerivedStruct. i want to be able to instantiate a...
4
by: KishorAditya | last post by:
Hi All, Consider the following scenario: class Top { }; class Left: virtual public Top { }; class Right: virtual public Top { }; class Bottom: public Left, public Right {}; Many books propose...
12
by: bgold | last post by:
Hey. I have a base class (SPRITE), and using this base class I have derived a large number of derived classes (PERSON, BULLET, MISSILE, etc.). Now, at a certain point in my program, I have a pair...
6
by: Bhawna | last post by:
I am into c++ code maintenance for last 3-4 years but recently I am put into design phase of a new project. Being a small comapany I dont have enough guidance from seniors. Currently I am into a...
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: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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.