By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,605 Members | 1,491 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,605 IT Pros & Developers. It's quick & easy.

Can i pass a function to template?:

P: n/a
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));
}

}
};

Nov 12 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
tomix wrote:
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));
}

}

};
Nov 12 '06 #2

P: n/a

tomix wrote:
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;
};

Nov 13 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.