473,395 Members | 1,870 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Can i pass a function to template?:

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
2 1286
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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Ioannis Vranos | last post by:
Is it possible to pass some template (e.g. vector) as an argument in a function and instantiate inside the function like this? T<int> con(1); --
6
by: david | last post by:
For example, I can call <xsl:value-of select="concat('name','(',value,')')"/>, is there anyway I can pass the expression concat('name','(',value,')') as a variable from outside the template? ...
23
by: Sanjay Kumar | last post by:
Folks, I am getting back into C++ after a long time and I have this simple question: How do pyou ass a STL container like say a vector or a map (to and from a function) ? function: ...
7
by: Jess | last post by:
Hello, I learned that when I work with templates in C++, I should have functions that pass arguments by reference because the type of object is not known. Does it mean that if I have a function...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.