Trying to templatize an algorithm.

T

tuko

Hello kind people.

I have the classes "point", "curve", "surface", "region" as shown
below. Every class has a member function that returns a list
of pointers to objects that comprise the *this object.

In main function I have implemented an algorithm
(some lines of code noted as "My Algorithm") which in
the current state works for "surface" and "curve" objects.

I am trying to templetize these lines of code to make them work for
"curve-point", "region-surface" e.t.c, but I have compilation problems.

I'm trying to pass as argument a pointer to the member function (get_***),
but obviously I'm doing it wrong. Can anyone explain me how to do this?

If the pairs are <region, surface> the passed pointer to member function should
be get_surfaces,
If the pairs are <surface, curve> the passed pointer to member function should
be get_curves, e.t.c.

Right now I have implemented all the algorithms I need, but the code is
very long and it is repeated for every pair of classes, and so I need it
to templatize them. I provide the minimum snippet which demonstrates
my error.

I hope that I am asking the question with clarity...

Many thanks for your time.

--------------------------------------------------------------------------
// main.hpp program begins here. This is line 1
class point {
};

class curve {
public:
std::list<point *> get_points() {return belongPoints;}
private:
std::list<point *> belongPoints;
};

class surface {
public:
std::list<curve *> get_curves() {return belongCurves;}
private:
std::list<curve *> belongCurves;
};

class region {
public:
std::list<surface *> get_surfaces() {return belongSurfaces;}
private:
std::list<surface *> belongSurfaces;
};

class prog {
public:
std::list<point *> m_Points;
std::list<point *> m_selPoints;
std::list<curve *> m_Curves;
std::list<curve *> m_selCurves;
std::list<surface *> m_Surfaces;
std::list<surface *> m_selSurfaces;
std::list<region *> m_Regions;
std::list<region *> m_selRegions;
};

template<class T1, class T2>
void templatize(std::list<T1 *> &obj1, std::list<T2 *>(*func)()) {
//
typename std::list<T1 *> :: iterator isr = obj1.begin();
while (isr!=obj1.end()) {
// std::list<T2 *> belongList = (*isr)->func();
isr++;
}
//
}
// main.hpp program ends. This is line 48
--------------------------------------------------------------------------
// main.cpp program begins. This is line 1
#include <list>
#include <algorithm>
//
#include "geom.hpp"

int main () {

prog *m_pDoc = new prog;

templatize<surface, curve>(m_pDoc->m_selSurfaces, &surface::get_curves);
// templatize<region, surface>(m_pDoc->m_selRegions, &region::get_surfaces);
// templatize<curve, point>(m_pDoc->m_selCurves, &curve::get_points);

//
// Section "My Algorithm" Begins
std::list<surface *> :: iterator isr = m_pDoc->m_selSurfaces.begin();
while (isr!=m_pDoc->m_selSurfaces.end()) {
std::list<curve *> curveList = (*isr)->get_curves();
std::list<curve *> :: iterator icr = curveList.begin();
while (icr!=curveList.end()) {
if (std::find(m_pDoc->m_selCurves.begin(),
m_pDoc->m_selCurves.end(), *icr)!=
m_pDoc->m_selCurves.end()) {
m_pDoc->m_selCurves.remove(*icr);
}
icr++;
}
isr++;
}
// Section "My Algorithm" Ends


return 0;
}
// Main program ends. This is line 36
 
D

David Hilsee

tuko said:
Hello kind people.

I have the classes "point", "curve", "surface", "region" as shown
below. Every class has a member function that returns a list
of pointers to objects that comprise the *this object.

In main function I have implemented an algorithm
(some lines of code noted as "My Algorithm") which in
the current state works for "surface" and "curve" objects.

I am trying to templetize these lines of code to make them work for
"curve-point", "region-surface" e.t.c, but I have compilation problems.

I'm trying to pass as argument a pointer to the member function (get_***),
but obviously I'm doing it wrong. Can anyone explain me how to do this?
template<class T1, class T2>
void templatize(std::list<T1 *> &obj1, std::list<T2 *>(*func)()) {

This is not the syntax you use for member function pointers; it's the syntax
one uses for non-member function pointers. You need to change

std::list<T2 *>(*func)()

to

std::list said:
//
typename std::list<T1 *> :: iterator isr = obj1.begin();
while (isr!=obj1.end()) {
// std::list<T2 *> belongList = (*isr)->func();

To invoke the member function via the pointer:

((*isr)->*func)()

isr++;
}
//
}
<snip>

HTH
 

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

Forum statistics

Threads
474,176
Messages
2,570,949
Members
47,500
Latest member
ArianneJsb

Latest Threads

Top