Can i pass a function to template?:

T

tomix

Hi,
I am trying to create a template which fill window list control with
items.

I would like to ask can i 'pass' to the template the actual function
which need
to be called on the item in order to get its name or description?

currently i assuming that the container is a map and the item have
GetName method:


template < class TControl,class TContainer>
class WindowLoaderFromMap
{
public:
static Load(TControl& wnd,TContainer& container)
{
for(TContainer::iterator
it=container.begin();it!=container.end();it++)
{
int index=wnd.AddString(it->second->GetName().c_str());
wnd.SetItemDataPtr(index,static_cast<void*>(it->second));
}

}


};
 
G

Gianni Mariani

tomix said:
Hi,
I am trying to create a template which fill window list control with
items.

I would like to ask can i 'pass' to the template the actual function
which need
to be called on the item in order to get its name or description?

currently i assuming that the container is a map and the item have
GetName method:


template < class TControl,class TContainer>
class WindowLoaderFromMap
{
public:
static Load(TControl& wnd,TContainer& container)
{
for(TContainer::iterator
it=container.begin();it!=container.end();it++)
{
int index=wnd.AddString(it->second->GetName().c_str());
wnd.SetItemDataPtr(index,static_cast<void*>(it->second));
}

}


};


I usually use template specialization for this. (I call it the moral
equivalent to the come-from).


template <typename T>
const char * WindowLoaderGetName( T & i_thing )
{
return i_thing->GetName().c_str();
}

class OtherType;
template <>
const char * WindowLoaderGetName( const OtherType * & i_thing )
{
return i_thing->OtherName().c_str();
}

// you could use classes instead of functions

template < class TControl,class TContainer>
class WindowLoaderFromMap
{
public:
static Load(TControl& wnd,TContainer& container)
{
for(
TContainer::iterator it=container.begin();
it!=container.end();
it++
)
{
int index=wnd.AddString(WindowLoaderGetName( it->second ));

wnd.SetItemDataPtr(index,static_cast<void*>(it->second));
}

}

};
 
D

dasjotre

tomix said:
Hi,
I am trying to create a template which fill window list control with
items.

I would like to ask can i 'pass' to the template the actual function
which need
to be called on the item in order to get its name or description?

currently i assuming that the container is a map and the item have
GetName method:


template < class TControl,class TContainer>
class WindowLoaderFromMap
{
public:
static Load(TControl& wnd,TContainer& container)
{
for(TContainer::iterator
it=container.begin();it!=container.end();it++)
{
int index=wnd.AddString(it->second->GetName().c_str());
wnd.SetItemDataPtr(index,static_cast<void*>(it->second));
}

}


};

The container must have a type of the
referent to be instantiated. So all your
containers ether have the same type or share
a common base and are kept by pointer of
some kind.

Make that common base inherit from this:

struct i_provide_name
{
virtual char const * GetName() const =0;
};
 

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
473,774
Messages
2,569,599
Members
45,162
Latest member
GertrudeMa
Top