tags:

views:

100

answers:

2

Hi, Can someone suggest me a way to map a template classes with a set of member functions from another class? Whenever i call one of the function inside a template class, it should call the associated member function of the other class.

Updating with a use-case

template<int walktype>
class Walker
{
   Node* node;

    bool walk()
    {
        switch(walktype)
        case 1:
            node->firstwalk();
        case 2:
            node->secondwalk();
        ......

    }
};

Please consider the above one as a pseudo-code. I want the switch-case decision to be taken at the compile time.

Thanks, Gokul.

+2  A: 

It seems you want it to be selected in compile time so you can especialize you class template like this:

// corresponding to if (walktype != 1) ... condition
template<int>
class Walker
{
    Node* node;

    bool walk()
    {
        node->secondwalk();
    }
};

// corresponding to if (walktype == 1) ... condition
template<>
class Walker<1>
{
    Node* node;
    bool walk()
    {
        node->firstwalk();
    }
};
fogo
Sorry, i am misunderstood. i have a set of classes to be mapped against a set of functions. Its just not one specialization
Gokul
And if instead of template classes you tried to make overloads for this set of classes? You seem to know which classes your `Walker` class will be used with and this will suffice your compile-time resolution, but I might have misunderstood you again. If you can give us more information, I'll happily edit my answer and try help :)
fogo
@fogo: I think boost::mpl::map would provide association between two sets of classes, but here i am trying to map a member function against each class. Your answer is fine, but i need to repeat the class definition every time for every template specilaization(lot of code copy/paste). i want to avoid that.
Gokul
A: 

I found a way to do it using boost::mpl::map. I need to create a type out of the function and use that type as a template parameter for the class and associate this class with the original class using boost::mpl::map.

Thanks, Gokul.

Gokul