variable number of class method variants

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 |
*-------------------------------------------------------------------*
 
R

Rolf Magnus

Leslaw said:
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.
 
D

Dervish

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;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top