我已经用“using”关键字声明了函数指针的别名,但我不知道如何使用该别名。
在类UpdateState
的函数中Person
,我想替换m_state
与当前状态和要转换到下一个状态相对应的函数的返回值。但是,以下错误发生在第 38 行Person.cpp
,我不知道如何纠正该行。我认为我错误地使用了别名数组。
error C2064: term does not evaluate to a function taking 1 arguments
Run Code Online (Sandbox Code Playgroud)
人.h
error C2064: term does not evaluate to a function taking 1 arguments
Run Code Online (Sandbox Code Playgroud)
人物.cpp
#pragma once
enum STATE
{
A,
B,
C,
D,
E,
STATE_NUM,
};
class Person
{
public:
Person();
~Person();
void UpdateState();
STATE IsInStateA(char nextState);
STATE IsInStateB(char nextState);
STATE IsInStateC(char nextState);
STATE IsInStateD(char nextState);
STATE IsInStateE(char nextState);
private:
STATE m_state;
};
Run Code Online (Sandbox Code Playgroud) 以下是程序的文本:
#include <stdio.h>
int f(int x){
return x+1;
}
int main() {
int (*p)(int) = f;
printf( "%d\n", sizeof(p) );
printf( "%d\n", sizeof(f) );
}
Run Code Online (Sandbox Code Playgroud)
该图像与 C 代码相关,我的问题仅针对 C。
这f
是函数的名称,并且p
是指向该函数的指针f
。
由于p
最终是一个地址,因此根据地址总线的大小,输出结果sizeof(p)
为真,这完全没问题。
这里f
也是指向函数f
。所以最终f
也是一个地址。
我的主要问题是为什么 的输出sizeof(f)
不是8,为什么是1以及如何是1?
我编写了自己的atexit方法,问题是,传入的所有内容都是1.当我尝试在将地址提供给我的atexit之前打印地址时,编译器生成了以下警告:
void dummy()将始终评估为'true'
void atexit(void(*func)(void))
{
cerr << func << endl; // prints 1
// store func for later
}
void dummy()
{
cout << "dummy()\n";
}
cerr << &dummy << endl; // prints 1, generates warning
atexit(&dummy);
Run Code Online (Sandbox Code Playgroud)
为什么总是试图传递真实而不是地址?注意:如果重要,真正的代码是试图传递类的静态私有成员函数(singleton)的地址.
编辑
我不在乎打印地址是什么,这是一个例子.我想弄清楚的是为什么地址作为布尔值传递给atexit,因此始终为1.
我是C++的新手(经过许多Java年).我正在尝试使用函数指针指向我的类的成员函数,如下所示:
class MyClass
{
public:
MyClass();
void foo();
void bar();
};
MyClass::MyClass(){}
void MyClass::bar(){}
void MyClass::foo()
{
void (MyClass::*myMethod)();
myMethod = &MyClass::bar;
//-----THIS IS THE LINE WITH PROBLEM-----------
myMethod();
}
Run Code Online (Sandbox Code Playgroud)
但是,编译器失败:
test.cpp: In member function ‘void MyClass::foo()’:
test.cpp:22:14: error: must use ‘.*’ or ‘->*’ to call pointer-to-member function in ‘myMethod (...)’, e.g. ‘(... ->* myMethod) (...)’
myMethod();
^
Run Code Online (Sandbox Code Playgroud)
我一直在尝试各种各样的*,&字符混合,并搜索解决方案,但无法让它工作.
任何的想法?
如这个小脚本所示.
#include <stdio.h>
struct student{
short count;
void (*addCount)();
};
void student_addCount(struct student a){
a.count++;
}
int main(){
struct student student;
student.addCount = student_addCount;
student.count = 0;
student.addCount();
student.addCount();
student.addCount();
student.addCount();
student.addCount();
printf("%d\n",student.count);
}
Run Code Online (Sandbox Code Playgroud)
我已经添加了一个指向结构内部函数的指针,但我不知道为什么这会起作用,因为函数'addCount'没有接收任何参数,它实际上累计了指定的次数.
我正在使用GCC 6.3.0在不同的环境中编译此代码,例如ideone.com,wandbox.org和WSL中的编译器.
这是证明它与ideone一起工作. https://ideone.com/Cam4xY
Can you do anything in Go with a *func()
?
var f func() = foo // works
var g *func() // works
g = foo // fails `cannot use foo (type func()) as type *func() in assignment` as expected
g = &foo // fails too `cannot take the address of foo`
Run Code Online (Sandbox Code Playgroud)