我想将一个函数指针设置为一个类的成员,该类是指向同一个类中另一个函数的指针.我这样做的原因很复杂.
在这个例子中,我希望输出为"1"
class A {
public:
int f();
int (*x)();
}
int A::f() {
return 1;
}
int main() {
A a;
a.x = a.f;
printf("%d\n",a.x())
}
Run Code Online (Sandbox Code Playgroud)
但这在编译时失败了.为什么?
struct B
{
void (B::*pf)(int, int); // data member
B () : pf(&B::foo) {}
void foo (int i, int j) { cout<<"foo(int, int)\n"; } // target method
};
int main ()
{
B obj;
// how to call foo() using obj.pf ?
}
Run Code Online (Sandbox Code Playgroud)
在上面的测试代码中,pf是一个数据成员B.调用它的语法规则是什么?它应该是直截了当的,但我没有得到正确的匹配.例如,如果我尝试,obj.*pf(0,0);那么我得到:
error: must use ‘.*’ or ‘->*’ to call pointer-to-member function in ‘pf (...)’, e.g. ‘(... ->* pf) (...)’
Run Code Online (Sandbox Code Playgroud) 我收到了一个我不明白的编译错误(MS VS 2008).在弄乱了好几个小时之后,这一切都很模糊,我觉得有一些非常明显(而且非常愚蠢)的东西,我很想念.这是基本代码:
typedef int (C::*PFN)(int);
struct MAP_ENTRY
{
int id;
PFN pfn;
};
class C
{
...
int Dispatch(int, int);
MAP_ENTRY *pMap;
...
};
int C::Dispatch(int id, int val)
{
for (MAP_ENTRY *p = pMap; p->id != 0; ++p)
{
if (p->id == id)
return p->pfn(val); // <--- error here
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
编译器在箭头处声称"术语不评估为采用1参数的函数".为什么不?PFN原型为带有一个参数的函数,MAP_ENTRY.pfn是PFN.我在这里错过了什么?
据我所知,每个创建的对象都有自己的地址,每个对象的方法也有自己的地址。我想用以下想法来验证这一点:
步骤1:构建具有公共方法的类A,其名称为“method”。
步骤2:在A类中创建两个对象,它们是对象“b”和对象“c”。
步骤3:使用函数指针访问“b.method”和“c.method”的地址,检查它们是否相等。
但我在步骤3中遇到了问题,并找到了各种方法来解决但失败了。所以我在这里发帖请教大家如何验证我上面所说的。感谢大家!这是我的 C++ 代码:
#include<iostream>
using namespace std;
class A
{
public:
int a;
void method()
{
//do something
}
static void (*fptr)();
};
int main()
{
A b, c;
A::fptr= &(b.method); //error: cannot convert 'A::method' from type
// 'void(A::)()' to type 'void (*)()'
cout << A::fptr << endl;
A::fptr= &(c.method); //error: cannot convert 'A::method' from type
//'void(A::)()' to type 'void (*)()'
cout << A::fptr << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud) 在我的项目中,MyClass.h文件中有几行
class MyClass{
public:
....
#ifndef __MAKECINT__
std::vector<Status_e(MyClass::*)(TupleInfo & info)> m_processObjects; //!
#endif
....
}
//The Status_e is an enum type
//The TupleInfo is a struct
Run Code Online (Sandbox Code Playgroud)
我*在上面的代码片段中无法理解这个用法.在MyClass.cxx文件中,m_processObjects用作:
for (unsigned int f = 0; f < m_processObjects.size(); ++f) {
status = (this->*m_processObjects[f])( m_tuples[i] );
if ( status == FAILURE ) {
return EL::StatusCode::FAILURE;
}
....
}
....
Run Code Online (Sandbox Code Playgroud)
我从来没有听说过这样的用法this->*blabla,但这段代码片段有效.那意味着什么?
好吧,我认为标题足够描述(但令人困惑,抱歉).
我正在读这个库:Timer1.
在头文件中有一个指向函数的公共成员指针,如下所示:
class TimerOne
{
public:
void (*isrCallback)();
};
Run Code Online (Sandbox Code Playgroud)
存在TimerOne类的实例化对象,称为"Timer1".
Timer1调用该函数如下:
Timer1.isrCallback();
Run Code Online (Sandbox Code Playgroud)
这怎么回事?我熟悉通过使用dereference运算符通过函数指针调用函数.
例如:
(*myFunc)();
Run Code Online (Sandbox Code Playgroud)
所以我希望通过对象的上述调用更像是:
(*Timer1.isrCallback)();
Run Code Online (Sandbox Code Playgroud)
那么,通过函数指针调用函数的可接受选项是什么,作为独立的函数指针和对象的成员?
这是我的代码的一部分:
在啊:
class classA
{
public:
void (*function_a)(void);
classA();
void function_a();
};
Run Code Online (Sandbox Code Playgroud)
在a.cpp中:
void classA::classA()
{
(*function_a)() = function_a;
}
void classA::function_a()
{
return;
}
Run Code Online (Sandbox Code Playgroud)
我想得到function_a的地址并将其保存到void(*function_a)(void),但是我得到编译错误"表达式不可分配".我该怎么做才能解决这个问题?
我在制作指向类方法的函数指针时遇到麻烦。我做了一个指向非类方法的函数指针,它工作正常。
int foo(){
return 5;
}
int main()
{
int (*pointer)() = foo;
std::cout << pointer();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我试图将其应用为使类中的实例变量成为函数指针。
这是头文件。它声明了变量方法将指向的私有方法Print。
class Game
{
public:
Game();
private:
void Print();
void (method)( void );
};
Run Code Online (Sandbox Code Playgroud)
Game构造函数尝试将指针方法分配给Print方法的地址。编译时,该行出现错误,提示“错误:必须调用对非静态成员函数的引用;”。我不知道那是什么意思 什么是实现此目的的正确方法?
Game::Game( void )
{
method = &Game::Print;
}
void Game::Print(){
std::cout << "PRINT";
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试在 C++ 类中使用函数指针,但出现错误。
#include <cstdio>
#include <iostream>
using namespace std;
class abc{
public:
void hello(){
printf("Hello world\n");
}
abc(){
void (*hw)(void);
hw = &hello;
hw();
}
}
int main()
{
abc ab;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
error: cannot convert ‘void (abc::*)()’ to ‘void (*)()’ in assignment
Run Code Online (Sandbox Code Playgroud)
但是以下代码适用于代码库中的我。任何人都可以帮我找出区别吗?
void hello(){
printf("Hello world\n");
}
int main()
{
void (*hw)(void);
hw = &hello;
hw();
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我已经用“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)