This question already has answers here:
How to call a function by its name (std::string) in C++?
(4 answers)
Closed 1 year ago.
I am very kindergartener to C++. Hope someone can help me out with my problem.
Assume there is a function defined in one class.
void __foo__(int x, int y){
//do something
}
In another class, there is a char pointer that holds the value of the function name.
static const char *func = "__foo__";
How do I call the function by using the "func" like func(0, 0)?
C++ doesn't have reflection, so you have to write the code to call the function based on the name yourself
void fall_function_based_on_name(const char* func_name, classWithMethod* self, lint x, int y) {
if (strcmp(func_name, "__foo__")==0)
self->__foo__(x, y);
else
throw std::logic_error("method name not found");
}
Related
This question already has answers here:
C++ Call Pointer To Member Function
(4 answers)
Function pointer to member function
(8 answers)
How to call through a member function pointer?
(2 answers)
Calling a function pointer on a member function [duplicate]
(1 answer)
Closed 17 days ago.
So I have a vector of a struct object containing a function pointer to an external class.
//Menu.h
#include "Byte.h"
struct menuItem
{
Byte (Byte::*funcPoint)(Byte&);
string descript;
};
class Menu
{
private:
vector<menuItem> menuVect;
void runSelection(Byte&, Byte&);
public:
Menu();
void addMenu(string Description, Byte (Byte::*f)(Byte&));
void runMenu(Byte&, Byte&);
void waitKey();
};
I can assign new instances of function pointers as seen here:
void Menu::addMenu(string Description, Byte(Byte::*f)(Byte&))
{
menuVect.push_back({f, Description});
}
But when it comes time execute any given function within the vector, I get an error
void Menu::runSelection(Byte& bX, Byte& bY)
{
int select;
cin >> select;
if (select > 0 && select < menuVect.size() + 1) {
cout << bX.toInt();
menuVect.at(select - 1).funcPoint();
}
else
exit(0);
waitKey();
}
Not sure if its necessary, but I'll include main as well, as one of the function pointers being assigned to the vector
Main
#include "Menu.h"
int main()
{
Menu m;
Byte b1(7);
Byte b2(3);
m.addMenu("1. Func 1", Byte::add);
m.addMenu("2. Func 2", Byte::sub);
m.addMenu("3. Func 3", Byte::mul);
m.addMenu("4. Func 4", Byte::div);
m.runMenu(b1, b2);
return 0;
}
Byte Byte::add(int val)
{
Byte b(this->toInt() + val);
return b;
}
I have already found an obvious work around, by simply not having the functions within an object
The particular errors I'm getting are:
Error (active) E0109 expression preceding parentheses of apparent call must have (pointer-to-) function type
(field)std::vector Menu::menuVect
Error C3867 'Byte::add': non-standard syntax; use '&' to create a pointer to member
But nothing I find when I look this up helps.
This question already has answers here:
Why does C++ allow us to surround the variable name in parentheses when declaring a variable?
(2 answers)
Closed 5 years ago.
class foo {
public:
bool operator () (int & i) {
return true;
}
};
int main() {
foo(WhyDoesThisCompile);
return 0;
}
When passing WhyDoesThisCompile (without spaces) to the functor, the program compiles.
Why is this? I tested it on clang 4.0.0.
You are not invoking the functor.
You are declaring a foo, called WhyDoesThisCompile.
Yes, despite the parentheses.
I guess you meant this:
foo()(WhyDoesThisCompile);
// ^^^^^
// temp ^^^^^^^^^^^^^^^^^^^^
// of invocation of op()
// type
// `foo`
… which doesn't.
This question already has answers here:
Returning a reference to a local variable in C++
(3 answers)
Closed 6 years ago.
Consider the following piece of program:
class cls
{
int vi;
public:
cls(int v=37)
{
vi=v;
}
friend int& f(cls);
};
int& f(cls c)
{
return c.vi;
}
int main()
{
const cls d(15);
f(d)=8;
cout<<f(d);
return 0;
}
When I run it, the output is
15
but I don't understand why 15, because I thought it should've outputed 8, because of the
f(d)=8
function, which from what I understand makes the c.vi=8, but I might be wrong and the function probably does something else entirely, so then I ask, what is the purpose or what does the
friend int& f(cls);
function do?
Your program has Undefined Behavior - you are returning a dangling reference to local variable of a function (argument is a local variable as well).
This question already has answers here:
How do I pass a reference to a two-dimensional array to a function?
(5 answers)
Reference to a Two-Dimesional Array
(2 answers)
Closed 8 years ago.
I have a problem. I dont know how to pass 2D array of pointers to fuction by reference.
class SomeClass
{
//body of class
}
void somefunction(SomeClass ***array)
{
//body of function
}
int main()
{
SomeClass * array[10][10]
someFunction(array?????)
}
Anyone know how to pass this array by reference??
Literally
void somefunction(SomeClass *(&array)[10][10])
{
}
int main()
{
SomeClass *array[10][10];
someFunction(array);
}
This question already has answers here:
Function pointer to different functions with different arguments in C
(6 answers)
Closed 9 years ago.
is there any possibility for function pointer for addressing function with different no of arguments of same return type, if not any alternate would be helpful.. thanks in advance
example:
struct method
{
char *name;
void (*ptr)(?); //? : what to define as arguments for this
};
void fun1(char *name)
{
printf("name %s\n\r",name);
}
void fun2(char *name, int a)
{
printf("name %s %d\n\r",name,a);
}
//defined before main()
method def[]=
{
{"fun1",fun1},
{"fun2",fun2}
}
//some where in main()
//call for function pointer
def[1].ptr("try", 2);
typedef void (*myfunc)(char *,int);
struct method
{
char *name;
myfunc ptr;
};
method def[]=
{
//we store fun1 as myfun
//type void(char*,int) and use it this way
{"fun1",(myfunc)fun1},
{"fun2",fun2}
};
This is by theory undefined behavior, but in reality it should work on most platforms
* edit -> this works on all plaforms just like printf(const char*,...) does.
In C, you can make your function pointer declaration read
void (*ptr)();
Which means 'A pointer to a function returning void and expecting an unspecified number of argments.'
With that adjustement, your sample program works as expected to me. However, it may well be that you're venturing into undefined (or at least implementation defined) lands here - I don't know for sure and I'm not a language lawyer (however there are plenty of language lawyers frequenting SO, so I'm sure somebody can whip up the relevant section of the standard or prove that there is none). So maybe you should rather use
/* Here be dragons! */
void (*ptr)();
instead.
Solution #1:
void fun1(char *name, ...);
void fun2(char *name, ...);
Solution #2:
method def[]=
{
{"fun1",printf},
{"fun2",printf}
}