473,511 Members | 15,046 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

variable number of class method variants


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
2 1111
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
> 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1443
by: Philip Rittenhouse | last post by:
I have discovered a couple of problems with the way the universal gateway code handles optional parameters and variable argument lists in COM servers. It appears to only be a problem when you...
16
25391
by: sneill | last post by:
How is it possible to take the value of a variable (in this case, MODE_CREATE, MODE_UPDATE, etc) and use that as an object property name? In the following example I want 'oIcon' object to have...
12
2912
by: Phil Certain | last post by:
Hi, I'm trying to do something very simple...or at least it should be. I have created a host page (gen.aspx) and a very simple user control (us.ascx). The corresponding code-behind files are...
11
3529
by: JohnR | last post by:
I'm trying to find a way to create a variable of a given type at runtime where I won't know the type until it actually executes. For example, dim x as object = "hi" x is declared as an object...
3
4324
by: carvalho.miguel | last post by:
hello, imagine you have a static class method that receives a function pointer, an int with the number of arguments and a variable number of arguments. in that static method you want to call...
7
1551
by: The Cool Giraffe | last post by:
Usually, when i declare a method and a variable in the header file i go as follows. public: int number; void doSome (); Then, in the CPP-file i will define it as follows. int number;
1
1613
sammyboy78
by: sammyboy78 | last post by:
I need to create a method for sorting my CD objects by the CD Title. I have no idea where to begin. I haven't yet covered any sorting algorithms so I need to know how I can sort this thing. I'm not...
2
874
by: anate | last post by:
I have 3 classes Class A & Class B and Class C CLASS A is a main class and it has a "seqno" static variable. Class A has a method with FOR LOOP which calls CLASS B method. CLASS B calls...
4
8875
by: Blackwater | last post by:
There's an old database system called "PICK" - still in use in various forms such as 'OpenQM' - wherein you can SELECT or READ directly into an array variable. For example : "SELECT * FROM...
0
7237
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,...
0
7137
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...
0
7506
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...
0
5659
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5063
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...
0
3219
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...
0
1572
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
780
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
445
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...

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.