Trying to templatize an algorithm.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • tuko

    Trying to templatize an algorithm.

    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 , &region::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.
  • David Hilsee

    #2
    Re: Trying to templatize an algorithm.

    "tuko" <tuko@away.co m> wrote in message news:ci1o1j$cj9 $1@nic.grnet.gr ...[color=blue]
    > 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?[/color]
    <snip>[color=blue]
    > template<class T1, class T2>
    > void templatize(std: :list<T1 *> &obj1, std::list<T2 *>(*func)()) {[/color]

    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<T2 *>(T1::*func)( )
    [color=blue]
    > //
    > typename std::list<T1 *> :: iterator isr = obj1.begin();
    > while (isr!=obj1.end( )) {
    > // std::list<T2 *> belongList = (*isr)->func();[/color]

    To invoke the member function via the pointer:

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

    [color=blue]
    > isr++;
    > }
    > //
    > }[/color]
    <snip>

    HTH

    --
    David Hilsee


    Comment

    Working...