473,757 Members | 2,081 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

OOP design problem with dynamic_cast



Hello,

I have the following OOP design problem. I have a base class
designed as an element of a list, let's say:

class A
{
public:
// some stuff

A* Next;
A* Prev;
}

in which pointers Next and Prev serve for accessing next and
previous elements in the list. For simplicity, I declare here all
contents as public.

Class A is abstract. In practice, real lists are constructed using
various kinds of derived classes, for example:
class B : public A
{
public:
// some stuff

B_Data *Data;
void foo(void);
}
where B_Data is some data specific to class B, and method foo
serves for operations on Data. However, I need to be able to access
in foo not only the data in a given object, but also in other objects
in the list. Thus, I need to do something like this:

void B::foo(void)
{
B* bnext = dynamic_cast<B* >(Next);
B* bprev = dynamic_cast<B* >(Prev);

Data = bnext->Data + bprev->Data; // for example

// etc.
}
My question is: Is there any neat possibility for redesigning
the above construction, in order to avoid dynamic casting in foo?
Dynamic casting is quite expensive and regarded as not elegant.
On the other hand, I don't want to make Data available already in A,
because I have several different kinds of derived classes B, which differ
somewhat by the behaviour, and by the data they hold.
The above seems to be a common problem in situations when
some of the functionality is shared by the classes, so that making
a common base A is reasonable, but some other functionality is not shared.

L. B.
*-------------------------------------------------------------------*
| Dr. Leslaw Bieniasz, |
| Institute of Physical Chemistry of the Polish Academy of Sciences,|
| Department of Electrochemical Oxidation of Gaseous Fuels, |
| ul. Zagrody 13, 30-318 Cracow, Poland. |
| tel./fax: +48 (12) 266-03-41 |
| E-mail: nb******@cyf-kr.edu.pl |
*-------------------------------------------------------------------*
| Interested in Computational Electrochemistr y? |
| Visit my web site: http://www.cyf-kr.edu.pl/~nbbienia |
*-------------------------------------------------------------------*
Jun 7 '06 #1
5 2459
Leslaw Bieniasz wrote:

Hello,

I have the following OOP design problem. I have a base class
designed as an element of a list, let's say:

class A
{
public:
// some stuff

A* Next;
A* Prev;
}
Don't forget the semicolon.

in which pointers Next and Prev serve for accessing next and
previous elements in the list. For simplicity, I declare here all
contents as public.

Class A is abstract. In practice, real lists are constructed using
various kinds of derived classes, for example:
Well, you didn't make A abstract, did you?


class B : public A
{
public:
// some stuff

B_Data *Data;
void foo(void);
}
(semicolon)


where B_Data is some data specific to class B, and method foo
serves for operations on Data. However, I need to be able to access
in foo not only the data in a given object, but also in other objects
in the list. Thus, I need to do something like this:

void B::foo(void)
{
B* bnext = dynamic_cast<B* >(Next);
B* bprev = dynamic_cast<B* >(Prev);

Data = bnext->Data + bprev->Data; // for example

// etc.
}
My question is: Is there any neat possibility for redesigning
the above construction, in order to avoid dynamic casting in foo?
Dynamic casting is quite expensive and regarded as not elegant.
On the other hand, I don't want to make Data available already in A,
because I have several different kinds of derived classes B, which differ
somewhat by the behaviour, and by the data they hold.
The above seems to be a common problem in situations when
some of the functionality is shared by the classes, so that making
a common base A is reasonable, but some other functionality is not shared.
I would offer two simple alternatives.

1) Use std::list from the standard library and get rid of A:

class B
{
public: void bar();
};

template <typename Itr>
void foo(Itr p)
{
Itr prev = p - 1;
Itr next = p + 1;

// play around *p, *prev and *next...
}

std::list<B> ls;
ls.push_back(B( ));
ls.push_back(B( ));
std::for_each(l s.begin(), ls.end(), std::mem_fun(B: :bar));

2) Make class A a class template:

template <typename T>
class A
{
public: // note: not encapsulating.. .
T* prev;
T* next;
T val;

template <typename U>
A(T* p, T* n, const U& u):val(u){}

virtual ~A(){}
};

class B
{
public:
void bar();
};

void foo(A<B>& node)
{
B& curr = node.val;
B& prev = node.prev->val;
B& next = ndoe.next->val;

// play around curr, prev and next
}

IMHO you should be more cautious when using inheritance. Generally this
kind of data structuring doesn't not fit into the domain of OOP so B
should not inherit from A, never.

L. B.


Regards,
Ben
Jun 7 '06 #2
template <typename T>
class A
{
public: // note: not encapsulating.. .
T* prev;
T* next;
T val;

template <typename U>
A(T* p, T* n, const U& u):val(u){}

virtual ~A(){}


Sorry, please ignore the line above. A's destructor should not be virtual.

Ben
Jun 7 '06 #3
In article <Pi************ *************** ****@kinga.cyf-kr.edu.pl>,
Leslaw Bieniasz <nb******@cyf-kr.edu.pl> wrote:
Hello,

I have the following OOP design problem. I have a base class
designed as an element of a list, let's say:

class A
{
public:
// some stuff

A* Next;
A* Prev;
}

in which pointers Next and Prev serve for accessing next and
previous elements in the list. For simplicity, I declare here all
contents as public.

Class A is abstract. In practice, real lists are constructed using
various kinds of derived classes, for example:
class B : public A
{
public:
// some stuff

B_Data *Data;
void foo(void);
}
where B_Data is some data specific to class B, and method foo
serves for operations on Data. However, I need to be able to access
in foo not only the data in a given object, but also in other objects
in the list. Thus, I need to do something like this:

void B::foo(void)
{
B* bnext = dynamic_cast<B* >(Next);
B* bprev = dynamic_cast<B* >(Prev);

Data = bnext->Data + bprev->Data; // for example

// etc.
}
My question is: Is there any neat possibility for redesigning
the above construction, in order to avoid dynamic casting in foo?
First, don't do it. Base classes with member-variables tend to be
brittle. And at any rate, is there *any* context where some client uses
one or more "A" objects without caring what sub-type they are?
Dynamic casting is quite expensive and regarded as not elegant.
On the other hand, I don't want to make Data available already in A,
because I have several different kinds of derived classes B, which differ
somewhat by the behaviour, and by the data they hold.
Use templates instead.
The above seems to be a common problem in situations when
some of the functionality is shared by the classes, so that making
a common base A is reasonable, but some other functionality is not shared.


You probably should be asking all this in the newsgroup "comp.objec t"
Jun 7 '06 #4
Leslaw Bieniasz wrote:

Hello,

I have the following OOP design problem. I have a base class
designed as an element of a list, let's say:

class A
{
public:
// some stuff

A* Next;
A* Prev;
}
Don't forget the semicolon.

in which pointers Next and Prev serve for accessing next and
previous elements in the list. For simplicity, I declare here all
contents as public.

Class A is abstract. In practice, real lists are constructed using
various kinds of derived classes, for example:
Well, you didn't make A abstract, did you?


class B : public A
{
public:
// some stuff

B_Data *Data;
void foo(void);
}
(semicolon)


where B_Data is some data specific to class B, and method foo
serves for operations on Data. However, I need to be able to access
in foo not only the data in a given object, but also in other objects
in the list. Thus, I need to do something like this:

void B::foo(void)
{
B* bnext = dynamic_cast<B* >(Next);
B* bprev = dynamic_cast<B* >(Prev);

Data = bnext->Data + bprev->Data; // for example

// etc.
}
My question is: Is there any neat possibility for redesigning
the above construction, in order to avoid dynamic casting in foo?
[snip]
I would offer two simple alternatives.

1) Use std::list from the standard library and get rid of A:

// note: code not tested

class B
{
public: void bar();
};

std::list<B> ls;
ls.push_back(B( ));
ls.push_back(B( ));
std::for_each(l s.begin(), ls.end(), std::mem_fun(B: :bar));

2) Make class A a class template:

// Note: code not tested

template <typename T>
class A
{
public: // note: not encapsulating.. .
T* prev;
T* next;
T val;

template <typename U>
A(T* p, T* n, const U& u):val(u){}

virtual ~A(){}
};

class B
{
public:
void bar();
};

void foo(A<B>& node) // example
{
A<B>* p = &node;

while (p != 0)
{
p->val.bar();
p = p->next;
}
}

IMHO you should be more cautious when using inheritance. Generally this
kind of data structuring doesn't not fit into the domain of OOP so B
should not inherit from A, never.

L. B.


Regards,
Ben
Jun 7 '06 #5

Leslaw Bieniasz wrote:
My question is: Is there any neat possibility for redesigning
the above construction, in order to avoid dynamic casting in foo?


Yes, in that you could completely rewrite it - you can call that
'redesign'. No, you can't get rid of the dynamic cast the way you are
going.

http://www.dcs.bbk.ac.uk/~keith/java...nheritance.pdf

DO NOT inherit for reuse purposes. Inherit when you have to because
you are actually that kind of object and you need to do some overriding
of virtual functions.

Jun 7 '06 #6

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

Similar topics

5
2433
by: Andrew Ward | last post by:
Hi All, Sorry if this is off topic, but I could not seem to find a suitable OO Design newsgroup. If there is one feel free to let me know. Here is a simplification of a general design problem I face. Say you have: class Car { virtual void speedup();
9
2465
by: Patchwork | last post by:
Hi Everyone, I have a design related question (in C++) that I am hoping someone can help me with. It is related to my previous post but since it was pointed out that I was more or less asking the wrong questions about the wrong 'topic' (polymorphism) I have posted this new question. Please don't see this as a spurious attempt to repost :-) As mentioned previously, there is a very real problem I am trying to solve, but I have reduced...
4
1609
by: Merlin | last post by:
Hi Imagine the following classes (A class diagram will help) BASE, A, B, C, D, E, F, G. A, B, C, D, G inherit from BASE. E, F inherit from D.
6
2448
by: Jacek Dziedzic | last post by:
Hello! First of all please forgive me for not posting a compilable snippet, but rather a simplified piece of code with the unimportant details left out. Let's say I have two classes 'box_shape' and 'cylinder_shape' that derive from a common base class 'shape', more or less like this: namespace Shapes {
11
1425
by: | last post by:
I have a rather newbie question regarding design of class hierarchy. Suppose I have a class Class0, and need to implement a public Class0.compute() interface. There are three different ways to choose the implementation, and the user of the class has to be able to make that choice. Here is what I have in mind. I declare three classes, ClassA, ClassB, ClassC, and each of them has a distinct compute() method that can be used by Class0. ...
31
1997
by: grahamo | last post by:
This came up in an interview I did a while ago and I wanted to know the correct answer. The setup is this; If I have a base class "food" and also two classes "meat" and "veg" that inherit from food, thus; food / \ / \ meat veg
3
1631
by: Steve | last post by:
Hi, I just know there must be some kind of well-known design pattern here, but I'll be damned if I can find it... Let me explain my situation. I have a hierarchy of classes for a GUI. All derived from class 'Primitive'. Fine so far.
0
1918
by: Siphiuel | last post by:
Hi everyone. When using visitor pattern, we have a nasty dependence on the types of visitable objects that is coded way on top on the visitor hierarchy. i mean, like this: class AbstractVisitor { public: virtual void visit(Object_type_1 *); virtual void visit(Object_type_2 *)
5
1696
by: ma740988 | last post by:
Consider: #include "handyfactory.h" #include <iostream> struct Shape { virtual void print() const=0; };
0
9487
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
9297
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
10069
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9884
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
9735
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...
1
7285
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
5168
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...
3
3395
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2697
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.