L
Leslaw Bieniasz
Cracow, 26.06.2006
Hi,
I have the following problem. I have a class, let's say A
(I omit irrelevant details)
class A
{
public:
A(void);
~A(void);
virtual void f(const int i);
virtual int n(void);
};
of which I have several derived classes, such as B, C, etc,
where function f() is overwritten.
The derived classes are used in the calling program in the following way:
B b();
for(int i=0; i<b.n(); i++)
{
// some stuff
b.f(i);
// some stuff
}
where the number of iterations depends on the derived class. Hence,
functions f() realise tasks dependent on i,
the number n of which also depends on the class.
One way of handling this design problem is to
define functions f() as follows:
void B::f(const int i)
{
if(i == 0)
{
// task 0
}
else
if(i == 1)
{
// task 1
}
else
if(i == 2)
{
// task 2
}
// etc.
return;
}
However, this solution is not very elegant.
I would prefer to use some sort of method
templates, for example:
class A
{
template<int i>virtual void f(void);
};
and then define a number of specialised function templates
for various i. However, in such a case there is a problem
with calling the templates in a loop written above, because
i is not a compile-time constant.
Is there any alternative elegant solution?
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 |
*-------------------------------------------------------------------*
Hi,
I have the following problem. I have a class, let's say A
(I omit irrelevant details)
class A
{
public:
A(void);
~A(void);
virtual void f(const int i);
virtual int n(void);
};
of which I have several derived classes, such as B, C, etc,
where function f() is overwritten.
The derived classes are used in the calling program in the following way:
B b();
for(int i=0; i<b.n(); i++)
{
// some stuff
b.f(i);
// some stuff
}
where the number of iterations depends on the derived class. Hence,
functions f() realise tasks dependent on i,
the number n of which also depends on the class.
One way of handling this design problem is to
define functions f() as follows:
void B::f(const int i)
{
if(i == 0)
{
// task 0
}
else
if(i == 1)
{
// task 1
}
else
if(i == 2)
{
// task 2
}
// etc.
return;
}
However, this solution is not very elegant.
I would prefer to use some sort of method
templates, for example:
class A
{
template<int i>virtual void f(void);
};
and then define a number of specialised function templates
for various i. However, in such a case there is a problem
with calling the templates in a loop written above, because
i is not a compile-time constant.
Is there any alternative elegant solution?
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 |
*-------------------------------------------------------------------*