x__x
我想做这样的事情:
typedef long (* fp)(BaseWindow< fp > & wnd, HWND hwnd, long wparam, long lparam);
Run Code Online (Sandbox Code Playgroud)
但我得到一个编译错误:
错误C2065:'fp':未声明的标识符
是否有可能以某种方式实现这一点?
这不是大多数人可能会使用的东西,但它只是浮现在脑海中并且在困扰着我.
是否有可能有一些机器代码,比如c-string,然后将其地址转换为函数指针,然后用它来运行该机器代码?
如何创建一个函数指针,它将函数指针作为参数(c ++)??? 我有这个代码
#include <iostream>
using namespace std;
int kvadrat (int a)
{
return a*a;
}
int kub (int a)
{
return a*a*a;
}
void centralna (int a, int (*pokfunk) (int))
{
int rezultat=(*pokfunk) (a);
cout<<rezultat<<endl;
}
void main ()
{
int a;
cout<<"unesite broj"<<endl;
cin>>a;
int (*pokfunk) (int) = 0;
if (a<10)
pokfunk=&kub;
if (a>=10)
pokfunk=&kvadrat;
void (*cent) (int, int*)=¢ralna; // here i have to create a pointer to the function "centralna" and call the function by its pointer …Run Code Online (Sandbox Code Playgroud) 在C++中有没有办法制作一个"untyed"函数指针?例如:
// pointer to global function
void foo( void (*fptr)() );
// pointer to member
void foo( void (Bar::*fptr)() );
Run Code Online (Sandbox Code Playgroud)
有没有办法可以删除该成员所在的班级?所以我可以这样做:
void foo( void ("any type"::*fptr)(), "same type as for the pointer" &instance );
Run Code Online (Sandbox Code Playgroud)
然后,在foo中,我想将该指针存储在一个列表中,这样我就可以在列表上进行迭代并调用指向的函数/成员,而不管它属于哪个类.当然,我需要一个可以调用该函数的实例列表.
谢谢.
可能重复:
是否可以在MATLAB中为每个文件定义多个函数?
为了在MATLAB中定义(非匿名)函数,您需要创建一个与函数同名的文件; 例如,一个名为myfunc的函数可以在文件myfunc.m中定义,如下所示:
function rtn = myfunc(arg)
% do some stuff
end
Run Code Online (Sandbox Code Playgroud)
假设在同一个文件myfunc.m中,我还有一个子函数,如
function rtn = myfunc(arg)
% do some stuff
end
function rtn = mysubfunc(arg)
% do some other stuff
end
Run Code Online (Sandbox Code Playgroud)
据我所知,没有办法访问mysubfunc从执行的以外的任何地方发生subfunc.m文件.在MATLAB(R2010b)中,我一直并且继续被这个小小的特质所困扰.我错了-有任何方法来调用mysubfunc从外部myfunc.m?
更新:新问题:有什么好办法吗?或者我应该真的只是吮吸它并继续制作更多文件?
所以我试图在一个数组中存储一系列方法(如果这是有道理的).
void *pointer[3];
pointer[0] = &[self rotate];
pointer[1] = &[self move];
pointer[2] = &[self attack];
//...
Run Code Online (Sandbox Code Playgroud)
我想要做的是有一个东西数组,并根据数组中对象的类型,调用某个方法.而不是有一堆if声明说:
if ([[myArray objectAtIndex:0] type] == robot]) {
//Do what robots do...
}
else if (...) {
}
else {
}
Run Code Online (Sandbox Code Playgroud)
在计时器中有这个我希望这样做:
pointer[[[myArray objectAtIndex:0] type]]; //This should invoke the appropriate method stored in the pointer.
Run Code Online (Sandbox Code Playgroud)
现在上面的代码说(第一个代码块):
Lvalue需要作为一元'&'操作数.
如果您需要任何澄清,请询问.
另外,只是为了让你知道我调用的所有方法都是void类型,没有任何参数.
我在这里使用了函数指针,但是无法理解它为什么不工作,这是我的代码片段.当我运行此代码时,我得到的不是输出,我期望在命令行中给出-r时打印降序和升序,如果没有给出命令行输入,则应该打印升序.
我的代码出了什么问题?
#include <stdio.h>
#include <stdlib.h>
void decending_sort() {
printf ("Decending order \n");
}
void ascending_sort() {
printf ("Ascending order \n");
}
int main( int argc, char **argv) {
int i;
void (*sort)();
while (*++argv) {
if ((strcmp ( *argv, "-r" )) == 0)
sort = decending_sort;
}
sort = ascending_sort;
}
Run Code Online (Sandbox Code Playgroud) 实际上我是编写处理程序的新手,但不知怎的,我设法编写了这段代码:
#include<iostream>
using namespace std;
class test
{
public:
typedef void (test::*MsgHandler)(int handle);
test()
{
cout<<"costructor called"<<endl;
}
void Initialize()
{
add_msg_Handler(4,&test::System);
}
void System(int handle)
{
cout<<endl<<"Inside System()"<<endl;
cout<<"handle:"<<handle<<endl;
}
protected:
MsgHandler message[20];
void add_msg_Handler(int idx,MsgHandler handler)
{
cout<<endl<<"Inside add_msg_Handler()"<<endl;
cout<<"handler:"<<handler<<endl;
message[idx]=handler;
cout<<"message:"<<message[idx]<<endl;
}
};
int main()
{
test obj;
obj.Initialize();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这段代码工作正常,我输出为:
costructor called
Inside add_msg_Handler()
handler:1
message:1
Run Code Online (Sandbox Code Playgroud)
但是我的范围之外还有一些事情.如果我是对的,应该在这一行中调用System():
add_msg_Handler(4,&test::System);
Run Code Online (Sandbox Code Playgroud)
但这不会发生.我需要帮助来纠正这个问题.
第二件事是,我无法理解为什么我得到这样的输出:
handler:1
Run Code Online (Sandbox Code Playgroud)
我的意思是处理程序如何初始化为1.可以帮助我解决这个问题?
我有一个功能,比方说
void processSomething(Arg1 arg1, Function t){
...
t(someVariable);
}
Run Code Online (Sandbox Code Playgroud)
我希望以下两种用法都能正常工作:
processSomething(myArg1, [&](SomeVariable someVar){...});
void(*myFunc)(void) = &someFunc;
processSomething(myArg1, myFunc);
Run Code Online (Sandbox Code Playgroud)
但是,我发现在使用void(*myFunc)(void)参数声明时我不能使用lambda-way .有没有两个单独的函数或过于复杂的包装器使用两种用法的方法?
我正在尝试理解通过指向函数指针数组的指针来调用函数的语法。我有一个函数指针数组FPTR arr[2],以及一个指向该数组的指针FPTR (vptr)[2]。但这在尝试通过指向数组的指针进行调用时给了我一个错误
typedef int (*FPTR)();
int func1(){
cout<<"func1() being called\n";
}
int func2(){
cout<<"fun2() being called\n";
}
FPTR arr[2] = {&func1,&func2};
FPTR (*vptr)[2];
vptr=&arr;
cout<<"\n"<<vptr[0]<<endl;
cout<<"\n"<<vptr[0]()<<endl; // ERROR when trying to call the first function
Run Code Online (Sandbox Code Playgroud)