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<surfa ce *> get_surfaces() {return belongSurfaces; }
private:
std::list<surfa ce *> 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<surfa ce *> m_Surfaces;
std::list<surfa ce *> m_selSurfaces;
std::list<regio n *> m_Regions;
std::list<regio n *> 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<surf ace, curve>(m_pDoc->m_selSurface s, &surface::get_c urves);
// templatize<regi on, surface>(m_pDoc->m_selRegions , ®ion::get_su rfaces);
// templatize<curv e, point>(m_pDoc->m_selCurves, &curve::get_poi nts);
//
// Section "My Algorithm" Begins
std::list<surfa ce *> :: 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_pD oc->m_selCurves.be gin(),
m_pDoc->m_selCurves.en d(), *icr)!=
m_pDoc->m_selCurves.en d()) {
m_pDoc->m_selCurves.re move(*icr);
}
icr++;
}
isr++;
}
// Section "My Algorithm" Ends
return 0;
}
// Main program ends. This is line 36
--------------------------------------------------------------------------
--
tuko, the ugly.
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<surfa ce *> get_surfaces() {return belongSurfaces; }
private:
std::list<surfa ce *> 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<surfa ce *> m_Surfaces;
std::list<surfa ce *> m_selSurfaces;
std::list<regio n *> m_Regions;
std::list<regio n *> 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<surf ace, curve>(m_pDoc->m_selSurface s, &surface::get_c urves);
// templatize<regi on, surface>(m_pDoc->m_selRegions , ®ion::get_su rfaces);
// templatize<curv e, point>(m_pDoc->m_selCurves, &curve::get_poi nts);
//
// Section "My Algorithm" Begins
std::list<surfa ce *> :: 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_pD oc->m_selCurves.be gin(),
m_pDoc->m_selCurves.en d(), *icr)!=
m_pDoc->m_selCurves.en d()) {
m_pDoc->m_selCurves.re move(*icr);
}
icr++;
}
isr++;
}
// Section "My Algorithm" Ends
return 0;
}
// Main program ends. This is line 36
--------------------------------------------------------------------------
--
tuko, the ugly.
Comment