Is storing any type of function in one variable possible?What are the differences between a pointer variable and a reference variable in C++?What's the difference between a method and a function?What is the naming convention in Python for variable and function names?var functionName = function() vs function functionName() What is the scope of variables in JavaScript?How do you check if a variable is an array in JavaScript?Set a default parameter value for a JavaScript functionHow to determine if variable is 'undefined' or 'null'?How to check if a variable is set in Bash?JavaScript check if variable exists (is defined/initialized)
What is this high flying aircraft over Pennsylvania?
Error in master's thesis, I do not know what to do
Friend wants my recommendation but I don't want to give it to him
Pre-Employment Background Check With Consent For Future Checks
Put the phone down / Put down the phone
1 John in Luther’s Bibel
Does capillary rise violate hydrostatic paradox?
Why does the frost depth increase when the surface temperature warms up?
Checking @@ROWCOUNT failing
Walter Rudin's mathematical analysis: theorem 2.43. Why proof can't work under the perfect set is uncountable.
Trouble reading roman numeral notation with flats
Center page as a whole without centering each element individually
I keep switching characters, how do I stop?
What is it called when someone votes for an option that's not their first choice?
Why doesn't Gödel's incompleteness theorem apply to false statements?
Asserting that Atheism and Theism are both faith based positions
Extract substring according to regexp with sed or grep
How can I, as DM, avoid the Conga Line of Death occurring when implementing some form of flanking rule?
What is the purpose of using a decision tree?
What (if any) is the reason to buy in small local stores?
Writing in a Christian voice
Can you describe someone as luxurious? As in someone who likes luxurious things?
Has the laser at Magurele, Romania reached a tenth of the Sun's power?
Why do Radio Buttons not fill the entire outer circle?
Is storing any type of function in one variable possible?
What are the differences between a pointer variable and a reference variable in C++?What's the difference between a method and a function?What is the naming convention in Python for variable and function names?var functionName = function() vs function functionName() What is the scope of variables in JavaScript?How do you check if a variable is an array in JavaScript?Set a default parameter value for a JavaScript functionHow to determine if variable is 'undefined' or 'null'?How to check if a variable is set in Bash?JavaScript check if variable exists (is defined/initialized)
I'm trying to make a menu array where each element is a struct that stores variables for text, key that needs to be pressed to select that item and function called on that key press (something like "Quit", 'Q', Quit()). I thought this would make things more efficient, but I can't find a way to make it work with varied function and parameter types (for example one item should be able to call a void function with no parameters, another a class int function with two parameters and so on). Is there a good way to do this or am I better off giving up on the idea?
Edit: Thank you all for your advice! The proposed solutions feel a little too complex for my newbie self, but attempting to understand them gave me some ideas! I ended up making the third variable hold an enum instead of a direct function call and then created a switch function that calls other functions based on that value.
c++ function variables
New contributor
add a comment |
I'm trying to make a menu array where each element is a struct that stores variables for text, key that needs to be pressed to select that item and function called on that key press (something like "Quit", 'Q', Quit()). I thought this would make things more efficient, but I can't find a way to make it work with varied function and parameter types (for example one item should be able to call a void function with no parameters, another a class int function with two parameters and so on). Is there a good way to do this or am I better off giving up on the idea?
Edit: Thank you all for your advice! The proposed solutions feel a little too complex for my newbie self, but attempting to understand them gave me some ideas! I ended up making the third variable hold an enum instead of a direct function call and then created a switch function that calls other functions based on that value.
c++ function variables
New contributor
2
You can use a lambda expression that calls your actual function
– Liran Funaro
19 hours ago
1
Where do the class instance and the two parameters come from?
– Bergi
15 hours ago
3
Look up the Command pattern. Note that I’m not saying not to use a function for this. But this pattern is the generalised form of what you need.
– Konrad Rudolph
14 hours ago
Note that a interface with a single method is roughly equivalent to astd::function
type.
– Caleth
14 hours ago
add a comment |
I'm trying to make a menu array where each element is a struct that stores variables for text, key that needs to be pressed to select that item and function called on that key press (something like "Quit", 'Q', Quit()). I thought this would make things more efficient, but I can't find a way to make it work with varied function and parameter types (for example one item should be able to call a void function with no parameters, another a class int function with two parameters and so on). Is there a good way to do this or am I better off giving up on the idea?
Edit: Thank you all for your advice! The proposed solutions feel a little too complex for my newbie self, but attempting to understand them gave me some ideas! I ended up making the third variable hold an enum instead of a direct function call and then created a switch function that calls other functions based on that value.
c++ function variables
New contributor
I'm trying to make a menu array where each element is a struct that stores variables for text, key that needs to be pressed to select that item and function called on that key press (something like "Quit", 'Q', Quit()). I thought this would make things more efficient, but I can't find a way to make it work with varied function and parameter types (for example one item should be able to call a void function with no parameters, another a class int function with two parameters and so on). Is there a good way to do this or am I better off giving up on the idea?
Edit: Thank you all for your advice! The proposed solutions feel a little too complex for my newbie self, but attempting to understand them gave me some ideas! I ended up making the third variable hold an enum instead of a direct function call and then created a switch function that calls other functions based on that value.
c++ function variables
c++ function variables
New contributor
New contributor
edited 9 hours ago
Rhyme
New contributor
asked 19 hours ago
RhymeRhyme
565
565
New contributor
New contributor
2
You can use a lambda expression that calls your actual function
– Liran Funaro
19 hours ago
1
Where do the class instance and the two parameters come from?
– Bergi
15 hours ago
3
Look up the Command pattern. Note that I’m not saying not to use a function for this. But this pattern is the generalised form of what you need.
– Konrad Rudolph
14 hours ago
Note that a interface with a single method is roughly equivalent to astd::function
type.
– Caleth
14 hours ago
add a comment |
2
You can use a lambda expression that calls your actual function
– Liran Funaro
19 hours ago
1
Where do the class instance and the two parameters come from?
– Bergi
15 hours ago
3
Look up the Command pattern. Note that I’m not saying not to use a function for this. But this pattern is the generalised form of what you need.
– Konrad Rudolph
14 hours ago
Note that a interface with a single method is roughly equivalent to astd::function
type.
– Caleth
14 hours ago
2
2
You can use a lambda expression that calls your actual function
– Liran Funaro
19 hours ago
You can use a lambda expression that calls your actual function
– Liran Funaro
19 hours ago
1
1
Where do the class instance and the two parameters come from?
– Bergi
15 hours ago
Where do the class instance and the two parameters come from?
– Bergi
15 hours ago
3
3
Look up the Command pattern. Note that I’m not saying not to use a function for this. But this pattern is the generalised form of what you need.
– Konrad Rudolph
14 hours ago
Look up the Command pattern. Note that I’m not saying not to use a function for this. But this pattern is the generalised form of what you need.
– Konrad Rudolph
14 hours ago
Note that a interface with a single method is roughly equivalent to a
std::function
type.– Caleth
14 hours ago
Note that a interface with a single method is roughly equivalent to a
std::function
type.– Caleth
14 hours ago
add a comment |
2 Answers
2
active
oldest
votes
There are actually a few ways of doing this.
One way is to use std::bind
to bind all functions to void func(void)
then you can store them equally.
The other way is to create a generic function/lambda which will call your function.
To store your functions you can use std::function
.
Also consider overriding operator()
of your classes.
add a comment |
The classic way of handling this is to have all the functions take the same parameters, and for those to be very flexible. For example, an integer or enum, and a pointer.
- your no-parameter function is passed -1 and
nullptr
and ignores them - your multi-parameter function casts the pointer to a pointer to some struct or class that holds all the bits and pieces it needs (and of course your calling code made that instance and passed its address)
The reason an enum or integer is hoisted out as one of the parameters is that "command type" is a super popular thing to need, so why do all that casting and extracting to get it?
If you have a performance problem as a result of this approach, then there are others, but this has literally been used for decades in Windows.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Rhyme is a new contributor. Be nice, and check out our Code of Conduct.
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%2f55238001%2fis-storing-any-type-of-function-in-one-variable-possible%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
There are actually a few ways of doing this.
One way is to use std::bind
to bind all functions to void func(void)
then you can store them equally.
The other way is to create a generic function/lambda which will call your function.
To store your functions you can use std::function
.
Also consider overriding operator()
of your classes.
add a comment |
There are actually a few ways of doing this.
One way is to use std::bind
to bind all functions to void func(void)
then you can store them equally.
The other way is to create a generic function/lambda which will call your function.
To store your functions you can use std::function
.
Also consider overriding operator()
of your classes.
add a comment |
There are actually a few ways of doing this.
One way is to use std::bind
to bind all functions to void func(void)
then you can store them equally.
The other way is to create a generic function/lambda which will call your function.
To store your functions you can use std::function
.
Also consider overriding operator()
of your classes.
There are actually a few ways of doing this.
One way is to use std::bind
to bind all functions to void func(void)
then you can store them equally.
The other way is to create a generic function/lambda which will call your function.
To store your functions you can use std::function
.
Also consider overriding operator()
of your classes.
answered 19 hours ago
Petar VelevPetar Velev
1,671619
1,671619
add a comment |
add a comment |
The classic way of handling this is to have all the functions take the same parameters, and for those to be very flexible. For example, an integer or enum, and a pointer.
- your no-parameter function is passed -1 and
nullptr
and ignores them - your multi-parameter function casts the pointer to a pointer to some struct or class that holds all the bits and pieces it needs (and of course your calling code made that instance and passed its address)
The reason an enum or integer is hoisted out as one of the parameters is that "command type" is a super popular thing to need, so why do all that casting and extracting to get it?
If you have a performance problem as a result of this approach, then there are others, but this has literally been used for decades in Windows.
add a comment |
The classic way of handling this is to have all the functions take the same parameters, and for those to be very flexible. For example, an integer or enum, and a pointer.
- your no-parameter function is passed -1 and
nullptr
and ignores them - your multi-parameter function casts the pointer to a pointer to some struct or class that holds all the bits and pieces it needs (and of course your calling code made that instance and passed its address)
The reason an enum or integer is hoisted out as one of the parameters is that "command type" is a super popular thing to need, so why do all that casting and extracting to get it?
If you have a performance problem as a result of this approach, then there are others, but this has literally been used for decades in Windows.
add a comment |
The classic way of handling this is to have all the functions take the same parameters, and for those to be very flexible. For example, an integer or enum, and a pointer.
- your no-parameter function is passed -1 and
nullptr
and ignores them - your multi-parameter function casts the pointer to a pointer to some struct or class that holds all the bits and pieces it needs (and of course your calling code made that instance and passed its address)
The reason an enum or integer is hoisted out as one of the parameters is that "command type" is a super popular thing to need, so why do all that casting and extracting to get it?
If you have a performance problem as a result of this approach, then there are others, but this has literally been used for decades in Windows.
The classic way of handling this is to have all the functions take the same parameters, and for those to be very flexible. For example, an integer or enum, and a pointer.
- your no-parameter function is passed -1 and
nullptr
and ignores them - your multi-parameter function casts the pointer to a pointer to some struct or class that holds all the bits and pieces it needs (and of course your calling code made that instance and passed its address)
The reason an enum or integer is hoisted out as one of the parameters is that "command type" is a super popular thing to need, so why do all that casting and extracting to get it?
If you have a performance problem as a result of this approach, then there are others, but this has literally been used for decades in Windows.
answered 14 hours ago
Kate GregoryKate Gregory
17.5k74881
17.5k74881
add a comment |
add a comment |
Rhyme is a new contributor. Be nice, and check out our Code of Conduct.
Rhyme is a new contributor. Be nice, and check out our Code of Conduct.
Rhyme is a new contributor. Be nice, and check out our Code of Conduct.
Rhyme is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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%2f55238001%2fis-storing-any-type-of-function-in-one-variable-possible%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
2
You can use a lambda expression that calls your actual function
– Liran Funaro
19 hours ago
1
Where do the class instance and the two parameters come from?
– Bergi
15 hours ago
3
Look up the Command pattern. Note that I’m not saying not to use a function for this. But this pattern is the generalised form of what you need.
– Konrad Rudolph
14 hours ago
Note that a interface with a single method is roughly equivalent to a
std::function
type.– Caleth
14 hours ago