How to store methods as function pointers in a map container?
up vote
11
down vote
favorite
I want to be able to call functions based on the data I read from file.
So for each item type, I want to call the desired reader method.
I wrote this code, but it does not compile where I want to add function pointers to the map. What is wrong?
#include <vector>
#include <map>
#include <iostream>
class reader
{
std::map< std::string, void(*)()> functionCallMap; // function pointer
void readA(){ std::cout << "reading An";};
void readB(){ std::cout << "reading Bn";};;
public:
reader()
{
*functionCallMap["A"] = &reader::readA;*
*functionCallMap["B"] = &reader::readB;*
}
void read()
{
auto (*f) = functionCallMap["A"];
(*f)();
}
};
I am filling the map at Constructor.
c++ function pointers
add a comment |
up vote
11
down vote
favorite
I want to be able to call functions based on the data I read from file.
So for each item type, I want to call the desired reader method.
I wrote this code, but it does not compile where I want to add function pointers to the map. What is wrong?
#include <vector>
#include <map>
#include <iostream>
class reader
{
std::map< std::string, void(*)()> functionCallMap; // function pointer
void readA(){ std::cout << "reading An";};
void readB(){ std::cout << "reading Bn";};;
public:
reader()
{
*functionCallMap["A"] = &reader::readA;*
*functionCallMap["B"] = &reader::readB;*
}
void read()
{
auto (*f) = functionCallMap["A"];
(*f)();
}
};
I am filling the map at Constructor.
c++ function pointers
5
A pointer to a non-member function is not the same as a pointer to a member function. The big difference is that member functions needs objects to be called on. You can solve it by usingstd::function
instead, together with either lambda expressions orstd::bind
.
– Some programmer dude
21 hours ago
add a comment |
up vote
11
down vote
favorite
up vote
11
down vote
favorite
I want to be able to call functions based on the data I read from file.
So for each item type, I want to call the desired reader method.
I wrote this code, but it does not compile where I want to add function pointers to the map. What is wrong?
#include <vector>
#include <map>
#include <iostream>
class reader
{
std::map< std::string, void(*)()> functionCallMap; // function pointer
void readA(){ std::cout << "reading An";};
void readB(){ std::cout << "reading Bn";};;
public:
reader()
{
*functionCallMap["A"] = &reader::readA;*
*functionCallMap["B"] = &reader::readB;*
}
void read()
{
auto (*f) = functionCallMap["A"];
(*f)();
}
};
I am filling the map at Constructor.
c++ function pointers
I want to be able to call functions based on the data I read from file.
So for each item type, I want to call the desired reader method.
I wrote this code, but it does not compile where I want to add function pointers to the map. What is wrong?
#include <vector>
#include <map>
#include <iostream>
class reader
{
std::map< std::string, void(*)()> functionCallMap; // function pointer
void readA(){ std::cout << "reading An";};
void readB(){ std::cout << "reading Bn";};;
public:
reader()
{
*functionCallMap["A"] = &reader::readA;*
*functionCallMap["B"] = &reader::readB;*
}
void read()
{
auto (*f) = functionCallMap["A"];
(*f)();
}
};
I am filling the map at Constructor.
c++ function pointers
c++ function pointers
edited 14 hours ago
jadarnel27
9,63362853
9,63362853
asked 21 hours ago
Ring Zero
837
837
5
A pointer to a non-member function is not the same as a pointer to a member function. The big difference is that member functions needs objects to be called on. You can solve it by usingstd::function
instead, together with either lambda expressions orstd::bind
.
– Some programmer dude
21 hours ago
add a comment |
5
A pointer to a non-member function is not the same as a pointer to a member function. The big difference is that member functions needs objects to be called on. You can solve it by usingstd::function
instead, together with either lambda expressions orstd::bind
.
– Some programmer dude
21 hours ago
5
5
A pointer to a non-member function is not the same as a pointer to a member function. The big difference is that member functions needs objects to be called on. You can solve it by using
std::function
instead, together with either lambda expressions or std::bind
.– Some programmer dude
21 hours ago
A pointer to a non-member function is not the same as a pointer to a member function. The big difference is that member functions needs objects to be called on. You can solve it by using
std::function
instead, together with either lambda expressions or std::bind
.– Some programmer dude
21 hours ago
add a comment |
3 Answers
3
active
oldest
votes
up vote
19
down vote
accepted
You can use std::function
with a lambda or std::bind
:
class reader
{
std::map<std::string, std::function<void()>> functionCallMap;
void readA() { std::cout << "reading An"; };
void readB() { std::cout << "reading Bn"; };
public:
reader()
{
functionCallMap["A"] = [this]() { readA(); };
functionCallMap["B"] = std::bind(&reader::readB, this);
}
void read()
{
functionCallMap["A"]();
functionCallMap["B"]();
}
};
I prefer your solution because it's more C++ish and cleaner than raw function pointer. Anyway lamba should be preferred to std::bind (Scott Meyer's Effective Modern C++)
– Moia
21 hours ago
Yes, It is a nice solution. But, according to Jason Turner, bind is expensive both in compile time and memory usage.
– Ring Zero
15 hours ago
@RingZero You should still use std::function instead of a function pointer, I would strongly advise switching the marked answer to this one for future readers. Using actual function pointers is bad for a number of reasons, but one issue is that you can't bind to anything that is a lambda pointer with variable capture. You will notice that std::function doesn't need the class pointer in the signature using the above.
– opa
11 hours ago
add a comment |
up vote
15
down vote
You need to use pointers to member functions, like this:
class reader
{
using FuncPtr = void(reader::*)(); // function pointer
std::map< std::string, FuncPtr> functionCallMap;
void readA(){ std::cout << "reading An"; }
void readB(){ std::cout << "reading Bn"; }
public:
reader()
{
functionCallMap["A"] = &reader::readA;
functionCallMap["B"] = &reader::readB;
}
void read()
{
auto f = functionCallMap["A"];
(this->*f)();
}
};
int main()
{
reader r;
r.read();
}
New contributor
Tried it and it works quite fine. Thanks.
– Ring Zero
15 hours ago
add a comment |
up vote
8
down vote
There are two answers so far, this and this.
The obvious difference is that one uses std::function
and other uses function pointers. This is not the important difference!!
The key point is that the member functions are non-static member functions. So, they are not of type void()
.
They are of type void(reader::*)()
. Thus, they can be only called if an object of type is reader is given; one can understand this somewhat as a hidden parameter.
The first answer just fixes the problem by specifying the correct type. This can be done using function pointers (as presented) or using std::function
(The latter is much more expensive!).
The second answer fixes the problem by binding the function pointer to the particular instance of the class. After binding, the type is then indeed void()
. This cannot be done using raw function pointers (because they can only point to a function and not an object/function pair!).
Also note that the timing of the binding is a design choice. I.E. does the application always want to bind the object storing the function pointers or potentially another one?
– Keith
11 hours ago
Four exclamation marks, zero answers.
– Carsten S
8 hours ago
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
19
down vote
accepted
You can use std::function
with a lambda or std::bind
:
class reader
{
std::map<std::string, std::function<void()>> functionCallMap;
void readA() { std::cout << "reading An"; };
void readB() { std::cout << "reading Bn"; };
public:
reader()
{
functionCallMap["A"] = [this]() { readA(); };
functionCallMap["B"] = std::bind(&reader::readB, this);
}
void read()
{
functionCallMap["A"]();
functionCallMap["B"]();
}
};
I prefer your solution because it's more C++ish and cleaner than raw function pointer. Anyway lamba should be preferred to std::bind (Scott Meyer's Effective Modern C++)
– Moia
21 hours ago
Yes, It is a nice solution. But, according to Jason Turner, bind is expensive both in compile time and memory usage.
– Ring Zero
15 hours ago
@RingZero You should still use std::function instead of a function pointer, I would strongly advise switching the marked answer to this one for future readers. Using actual function pointers is bad for a number of reasons, but one issue is that you can't bind to anything that is a lambda pointer with variable capture. You will notice that std::function doesn't need the class pointer in the signature using the above.
– opa
11 hours ago
add a comment |
up vote
19
down vote
accepted
You can use std::function
with a lambda or std::bind
:
class reader
{
std::map<std::string, std::function<void()>> functionCallMap;
void readA() { std::cout << "reading An"; };
void readB() { std::cout << "reading Bn"; };
public:
reader()
{
functionCallMap["A"] = [this]() { readA(); };
functionCallMap["B"] = std::bind(&reader::readB, this);
}
void read()
{
functionCallMap["A"]();
functionCallMap["B"]();
}
};
I prefer your solution because it's more C++ish and cleaner than raw function pointer. Anyway lamba should be preferred to std::bind (Scott Meyer's Effective Modern C++)
– Moia
21 hours ago
Yes, It is a nice solution. But, according to Jason Turner, bind is expensive both in compile time and memory usage.
– Ring Zero
15 hours ago
@RingZero You should still use std::function instead of a function pointer, I would strongly advise switching the marked answer to this one for future readers. Using actual function pointers is bad for a number of reasons, but one issue is that you can't bind to anything that is a lambda pointer with variable capture. You will notice that std::function doesn't need the class pointer in the signature using the above.
– opa
11 hours ago
add a comment |
up vote
19
down vote
accepted
up vote
19
down vote
accepted
You can use std::function
with a lambda or std::bind
:
class reader
{
std::map<std::string, std::function<void()>> functionCallMap;
void readA() { std::cout << "reading An"; };
void readB() { std::cout << "reading Bn"; };
public:
reader()
{
functionCallMap["A"] = [this]() { readA(); };
functionCallMap["B"] = std::bind(&reader::readB, this);
}
void read()
{
functionCallMap["A"]();
functionCallMap["B"]();
}
};
You can use std::function
with a lambda or std::bind
:
class reader
{
std::map<std::string, std::function<void()>> functionCallMap;
void readA() { std::cout << "reading An"; };
void readB() { std::cout << "reading Bn"; };
public:
reader()
{
functionCallMap["A"] = [this]() { readA(); };
functionCallMap["B"] = std::bind(&reader::readB, this);
}
void read()
{
functionCallMap["A"]();
functionCallMap["B"]();
}
};
answered 21 hours ago
Siliace
38615
38615
I prefer your solution because it's more C++ish and cleaner than raw function pointer. Anyway lamba should be preferred to std::bind (Scott Meyer's Effective Modern C++)
– Moia
21 hours ago
Yes, It is a nice solution. But, according to Jason Turner, bind is expensive both in compile time and memory usage.
– Ring Zero
15 hours ago
@RingZero You should still use std::function instead of a function pointer, I would strongly advise switching the marked answer to this one for future readers. Using actual function pointers is bad for a number of reasons, but one issue is that you can't bind to anything that is a lambda pointer with variable capture. You will notice that std::function doesn't need the class pointer in the signature using the above.
– opa
11 hours ago
add a comment |
I prefer your solution because it's more C++ish and cleaner than raw function pointer. Anyway lamba should be preferred to std::bind (Scott Meyer's Effective Modern C++)
– Moia
21 hours ago
Yes, It is a nice solution. But, according to Jason Turner, bind is expensive both in compile time and memory usage.
– Ring Zero
15 hours ago
@RingZero You should still use std::function instead of a function pointer, I would strongly advise switching the marked answer to this one for future readers. Using actual function pointers is bad for a number of reasons, but one issue is that you can't bind to anything that is a lambda pointer with variable capture. You will notice that std::function doesn't need the class pointer in the signature using the above.
– opa
11 hours ago
I prefer your solution because it's more C++ish and cleaner than raw function pointer. Anyway lamba should be preferred to std::bind (Scott Meyer's Effective Modern C++)
– Moia
21 hours ago
I prefer your solution because it's more C++ish and cleaner than raw function pointer. Anyway lamba should be preferred to std::bind (Scott Meyer's Effective Modern C++)
– Moia
21 hours ago
Yes, It is a nice solution. But, according to Jason Turner, bind is expensive both in compile time and memory usage.
– Ring Zero
15 hours ago
Yes, It is a nice solution. But, according to Jason Turner, bind is expensive both in compile time and memory usage.
– Ring Zero
15 hours ago
@RingZero You should still use std::function instead of a function pointer, I would strongly advise switching the marked answer to this one for future readers. Using actual function pointers is bad for a number of reasons, but one issue is that you can't bind to anything that is a lambda pointer with variable capture. You will notice that std::function doesn't need the class pointer in the signature using the above.
– opa
11 hours ago
@RingZero You should still use std::function instead of a function pointer, I would strongly advise switching the marked answer to this one for future readers. Using actual function pointers is bad for a number of reasons, but one issue is that you can't bind to anything that is a lambda pointer with variable capture. You will notice that std::function doesn't need the class pointer in the signature using the above.
– opa
11 hours ago
add a comment |
up vote
15
down vote
You need to use pointers to member functions, like this:
class reader
{
using FuncPtr = void(reader::*)(); // function pointer
std::map< std::string, FuncPtr> functionCallMap;
void readA(){ std::cout << "reading An"; }
void readB(){ std::cout << "reading Bn"; }
public:
reader()
{
functionCallMap["A"] = &reader::readA;
functionCallMap["B"] = &reader::readB;
}
void read()
{
auto f = functionCallMap["A"];
(this->*f)();
}
};
int main()
{
reader r;
r.read();
}
New contributor
Tried it and it works quite fine. Thanks.
– Ring Zero
15 hours ago
add a comment |
up vote
15
down vote
You need to use pointers to member functions, like this:
class reader
{
using FuncPtr = void(reader::*)(); // function pointer
std::map< std::string, FuncPtr> functionCallMap;
void readA(){ std::cout << "reading An"; }
void readB(){ std::cout << "reading Bn"; }
public:
reader()
{
functionCallMap["A"] = &reader::readA;
functionCallMap["B"] = &reader::readB;
}
void read()
{
auto f = functionCallMap["A"];
(this->*f)();
}
};
int main()
{
reader r;
r.read();
}
New contributor
Tried it and it works quite fine. Thanks.
– Ring Zero
15 hours ago
add a comment |
up vote
15
down vote
up vote
15
down vote
You need to use pointers to member functions, like this:
class reader
{
using FuncPtr = void(reader::*)(); // function pointer
std::map< std::string, FuncPtr> functionCallMap;
void readA(){ std::cout << "reading An"; }
void readB(){ std::cout << "reading Bn"; }
public:
reader()
{
functionCallMap["A"] = &reader::readA;
functionCallMap["B"] = &reader::readB;
}
void read()
{
auto f = functionCallMap["A"];
(this->*f)();
}
};
int main()
{
reader r;
r.read();
}
New contributor
You need to use pointers to member functions, like this:
class reader
{
using FuncPtr = void(reader::*)(); // function pointer
std::map< std::string, FuncPtr> functionCallMap;
void readA(){ std::cout << "reading An"; }
void readB(){ std::cout << "reading Bn"; }
public:
reader()
{
functionCallMap["A"] = &reader::readA;
functionCallMap["B"] = &reader::readB;
}
void read()
{
auto f = functionCallMap["A"];
(this->*f)();
}
};
int main()
{
reader r;
r.read();
}
New contributor
edited 11 hours ago
New contributor
answered 21 hours ago
snake_style
44536
44536
New contributor
New contributor
Tried it and it works quite fine. Thanks.
– Ring Zero
15 hours ago
add a comment |
Tried it and it works quite fine. Thanks.
– Ring Zero
15 hours ago
Tried it and it works quite fine. Thanks.
– Ring Zero
15 hours ago
Tried it and it works quite fine. Thanks.
– Ring Zero
15 hours ago
add a comment |
up vote
8
down vote
There are two answers so far, this and this.
The obvious difference is that one uses std::function
and other uses function pointers. This is not the important difference!!
The key point is that the member functions are non-static member functions. So, they are not of type void()
.
They are of type void(reader::*)()
. Thus, they can be only called if an object of type is reader is given; one can understand this somewhat as a hidden parameter.
The first answer just fixes the problem by specifying the correct type. This can be done using function pointers (as presented) or using std::function
(The latter is much more expensive!).
The second answer fixes the problem by binding the function pointer to the particular instance of the class. After binding, the type is then indeed void()
. This cannot be done using raw function pointers (because they can only point to a function and not an object/function pair!).
Also note that the timing of the binding is a design choice. I.E. does the application always want to bind the object storing the function pointers or potentially another one?
– Keith
11 hours ago
Four exclamation marks, zero answers.
– Carsten S
8 hours ago
add a comment |
up vote
8
down vote
There are two answers so far, this and this.
The obvious difference is that one uses std::function
and other uses function pointers. This is not the important difference!!
The key point is that the member functions are non-static member functions. So, they are not of type void()
.
They are of type void(reader::*)()
. Thus, they can be only called if an object of type is reader is given; one can understand this somewhat as a hidden parameter.
The first answer just fixes the problem by specifying the correct type. This can be done using function pointers (as presented) or using std::function
(The latter is much more expensive!).
The second answer fixes the problem by binding the function pointer to the particular instance of the class. After binding, the type is then indeed void()
. This cannot be done using raw function pointers (because they can only point to a function and not an object/function pair!).
Also note that the timing of the binding is a design choice. I.E. does the application always want to bind the object storing the function pointers or potentially another one?
– Keith
11 hours ago
Four exclamation marks, zero answers.
– Carsten S
8 hours ago
add a comment |
up vote
8
down vote
up vote
8
down vote
There are two answers so far, this and this.
The obvious difference is that one uses std::function
and other uses function pointers. This is not the important difference!!
The key point is that the member functions are non-static member functions. So, they are not of type void()
.
They are of type void(reader::*)()
. Thus, they can be only called if an object of type is reader is given; one can understand this somewhat as a hidden parameter.
The first answer just fixes the problem by specifying the correct type. This can be done using function pointers (as presented) or using std::function
(The latter is much more expensive!).
The second answer fixes the problem by binding the function pointer to the particular instance of the class. After binding, the type is then indeed void()
. This cannot be done using raw function pointers (because they can only point to a function and not an object/function pair!).
There are two answers so far, this and this.
The obvious difference is that one uses std::function
and other uses function pointers. This is not the important difference!!
The key point is that the member functions are non-static member functions. So, they are not of type void()
.
They are of type void(reader::*)()
. Thus, they can be only called if an object of type is reader is given; one can understand this somewhat as a hidden parameter.
The first answer just fixes the problem by specifying the correct type. This can be done using function pointers (as presented) or using std::function
(The latter is much more expensive!).
The second answer fixes the problem by binding the function pointer to the particular instance of the class. After binding, the type is then indeed void()
. This cannot be done using raw function pointers (because they can only point to a function and not an object/function pair!).
edited 17 hours ago
pushkin
3,673102450
3,673102450
answered 20 hours ago
Handy999
812
812
Also note that the timing of the binding is a design choice. I.E. does the application always want to bind the object storing the function pointers or potentially another one?
– Keith
11 hours ago
Four exclamation marks, zero answers.
– Carsten S
8 hours ago
add a comment |
Also note that the timing of the binding is a design choice. I.E. does the application always want to bind the object storing the function pointers or potentially another one?
– Keith
11 hours ago
Four exclamation marks, zero answers.
– Carsten S
8 hours ago
Also note that the timing of the binding is a design choice. I.E. does the application always want to bind the object storing the function pointers or potentially another one?
– Keith
11 hours ago
Also note that the timing of the binding is a design choice. I.E. does the application always want to bind the object storing the function pointers or potentially another one?
– Keith
11 hours ago
Four exclamation marks, zero answers.
– Carsten S
8 hours ago
Four exclamation marks, zero answers.
– Carsten S
8 hours ago
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53336880%2fhow-to-store-methods-as-function-pointers-in-a-map-container%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
5
A pointer to a non-member function is not the same as a pointer to a member function. The big difference is that member functions needs objects to be called on. You can solve it by using
std::function
instead, together with either lambda expressions orstd::bind
.– Some programmer dude
21 hours ago