473,479 Members | 2,117 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Containers containing pointers to functions

Hi,

How can I declare the type of pointer to a function ??

I want to create a map (from STL), which contains pointers to functions
as values, but I have to declare the type:

void print_red(){
cout << "red" << endl;
}

void print_green(){
cout << "green" << endl;
}

void print_blue(){
cout << "blue" << endl;
}

map <string, "pointer to a function"> m;

m["red"] = print_red;
m["green"] = print_green;
m["blue"] = print_blue;

How can I specify the type of this function. The same problem exists
when you want to declare a function, which take a pointer to another
function as an argument.
How can this be done??

Thanks Boris

Jul 19 '05 #1
2 1656
> Hi,

How can I declare the type of pointer to a function ??

I want to create a map (from STL), which contains pointers to functions
as values, but I have to declare the type:

void print_red(){
cout << "red" << endl;
}

void print_green(){
cout << "green" << endl;
}

void print_blue(){
cout << "blue" << endl;
}

map <string, "pointer to a function"> m;

m["red"] = print_red;
m["green"] = print_green;
m["blue"] = print_blue;


#include <map>
#include <string>
#include <iostream>

using namespace std;

void print_red(){
cout << "red" << endl;
}

void print_green(){
cout << "green" << endl;
}

void print_blue(){
cout << "blue" << endl;
}

map<string, void (*)()> functions;

int main() {
functions["red"] = print_red;
functions["green"] = print_green;
functions["blue"] = print_blue;

functions["red"]();
}

/ Erik
Jul 19 '05 #2
Ok,

Thanks to all your answers ! It works fine, with one exception:

specifying the return type of funtions, which return pointers to functions.

is the following code correct (my compiler gives me a parse error)?

int func(){
return 5;
}

int (*)() func2(){ // here is the parse error !!
return &func;
}

int main(){
int (*)() ptr = func2();
cout << ptr() << endl;
}
It works fine, when I use typedef but not the way I did above.

greets Boris

Jul 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
3707
by: Koen | last post by:
Hi! I have a vector containing the following: vector<Base*> theVector; theVector.push_back(new Der1()); theVector.push_back(new Der1()); theVector.push_back(new Der2());...
6
3355
by: Gandalf | last post by:
Hello. I have some questions about the standard containers. How does the standard containers behave if I do queue<Foo> myQ; queue<Foo> myQ2; .... insert into myQ... myQ = myQ2;
3
2182
by: JackC | last post by:
Hi Problem: I wish to use a pimpl to hide implementation/storage of a class (T), but I also want to hold objects of that class (T) in an std::vector<T> (or similar). T is non trivial (i.e. not...
6
2350
by: Mark | last post by:
If you have STL containers (like list, vector,...) in functions as automatic variables, do the nodes that are put on the containers get buffered so they can be reused by later container...
18
3018
by: Matthias Kaeppler | last post by:
Hi, in my program, I have to sort containers of objects which can be 2000 items big in some cases. Since STL containers are based around copying and since I need to sort these containers quite...
8
5088
by: Ross A. Finlayson | last post by:
I'm trying to write some C code, but I want to use C++'s std::vector. Indeed, if the code is compiled as C++, I want the container to actually be std::vector, in this case of a collection of value...
18
2171
by: Simon | last post by:
Hi, I understand what one the differences between std::vector, std::deque and std::list is, the std::vector can have data inserted/deleted at the end. The std::deque can have data...
15
4045
by: Nindi73 | last post by:
HI If I define the class DoubleMap such that struct DoubleMap : public std::map<std::string, double>{}; Is there any overhead in calling std::map member functions ? Moreover are STL...
14
1622
by: Remo D. | last post by:
I've read with much interest the threads on how to create data containers in C. I started thinking if simpler ADT (other than the proposed List) could be used as a starting point. What about...
0
7033
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6903
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
7027
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
7071
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6726
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...
1
4763
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
2974
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1291
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
557
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.