L
Leslaw Bieniasz
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: (e-mail address removed) |
*-------------------------------------------------------------------*
| Interested in Computational Electrochemistry? |
| Visit my web site: http://www.cyf-kr.edu.pl/~nbbienia |
*-------------------------------------------------------------------*
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: (e-mail address removed) |
*-------------------------------------------------------------------*
| Interested in Computational Electrochemistry? |
| Visit my web site: http://www.cyf-kr.edu.pl/~nbbienia |
*-------------------------------------------------------------------*