A small problem

J

J Anderson

Hi There,


I have a lot of code that looks like this:
//(The example below is not real.)


void func1(...)
{
for (...)
{
for (int n=0; n<size; n++)
{
if (A[B->m_a]->method3(C[D->m_b[n]]))
{
...
}
}
}
}


void func2(...)
{
for (...)
{
for (int n=0; n<size; n++)
{
//D and B are swapped and that is the only difference
if (A[D->m_b[n]]->method3(C[B->m_a]))
{
...
}
}
}
}

The code in the … is the same in all methods. I have about 4 or 5 of
these methods where the format is exactly the same, but a few parameters
are swapped around. For maintenance reasons I’d like to make them into
one function using templates (or something), however I don’t want to
sacrifice efficiency. I tried the following, which I didn’t think would
work (and doesn’t), because (as far as I know) you can't pass objects
other then constant integers vir templates..

template <class X, class Y>
void funcX(...)
{
for (...)
{
for (int n=0; n<size; n++)
{
if (A[X]->method2(C[Y]))
{
...
}
}
}
}

A call would be like:
funcX<D->m_b[n], B->m_a>(...);

or

funcX<B->m_a, D->m_b[n]>(...);

I know I could do it with # macros, but that seems messy to me (unless
you can show me something neat). I don’t really have a choice about
changing the rest of the design.

Any help would be greatly appreciated.

Thanks
 
R

Rob Williscroft

J Anderson wrote in
Hi There,

I have a lot of code that looks like this:>
//(The example below is not real> .)>

void func1(...)
{
for (...)
{
for (int n=0; n<size; n++)
{
if (A[B->m_a]->method3(C[D->m_b[n]]))
{
...
}
}
}
}

[snip]

template <typename First, typename Second>
void func_1or2(First *first, Second *second, ...)
{
for (...)
{
for (int n=0; n<size; n++)
{
if (A[first->m_b[n]]->method3(C[second->m_a]))
{
...
}
}
}

HTH


Rob.
 
J

J Anderson

Rob said:
J Anderson wrote in
Hi There,

I have a lot of code that looks like this:>

//(The example below is not real> .)>


void func1(...)
{
for (...)
{
for (int n=0; n<size; n++)
{
if (A[B->m_a]->method3(C[D->m_b[n]]))
{
...
}
}
}
}


[snip]

template <typename First, typename Second>
void func_1or2(First *first, Second *second, ...)
{
for (...)
{
for (int n=0; n<size; n++)
{
if (A[first->m_b[n]]->method3(C[second->m_a]))
{
...
}
}
}

HTH


Rob.

Thanks, but that won't work. first and second are part of the method,
not parmeters. Second actually changes each round of the first for-loop.
 
R

Rob Williscroft

J Anderson wrote in
Thanks, but that won't work. first and second are part of the method,
not parmeters. Second actually changes each round of the first for-loop.

Then please post an example that shows the real problem, preferably
with no ...'s.

Nobody can answer question's you dont ask.

Rob.
 

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
474,141
Messages
2,570,817
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top