By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,918 Members | 2,240 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,918 IT Pros & Developers. It's quick & easy.

variable number of class method variants

P: n/a

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: nb******@cyf-kr.edu.pl |
*-------------------------------------------------------------------*
| Interested in Computational Electrochemistry? |
| Visit my web site: http://www.cyf-kr.edu.pl/~nbbienia |
*-------------------------------------------------------------------*
Jun 26 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Leslaw Bieniasz wrote:

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);
You should probably make the destructor virtual, too. Note that adding a
dummy void parameter is not necessary in C++ to denote that a function
doesn't take any arguments.
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();
This declares a function called b that takes no arguments and returns a B.
Probably you meant:

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;
}
I would use switch/case instead of an if/else cascade.
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.
Yes. Since i is not a compile-time constant, you have to do the check at
run-time.
Is there any alternative elegant solution?


You could use an array of pointers to member functions and then use i as an
index into that array, but I don't really think this is more elegant than a
switch/case that calls the functions.

Jun 26 '06 #2

P: n/a
> 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?

Please note that for member functions template and vurtual are
mutually exclusive.

Looks you are trying to mix run-time and compile time approach. Below
is fully compile time generated code. So there is no for() and no
b.n().
class MyB
{
public:

template <int i> void iterate(){
f<i>();
iterate<i-1>();
}

template <> void iterate<0>(){
f<0>();
}

template <int i> void f();
template <> void f<0>(){
cout << 0 << endl;
}
template <> void f<1>(){
cout << 1 << endl;
}
};

int main ()
{
MyB b;
b.iterate<1>();

return 0;
}

Jun 26 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.