说你有:
struct c_struct {
int value;
/* other stuff */
void (* dump)();
};
Run Code Online (Sandbox Code Playgroud)
而且你想在某个时候:
c_struct_obj->dump();
Run Code Online (Sandbox Code Playgroud)
我假设你无法实例化一个c_struct对象,使得它的特定"转储"函数知道它的特定"值",就像C++方法知道成员变量一样(通过隐含的"这个"我想)?我想我已经知道了答案("不").如果是这样,是否有其他方式以OOPy方式使用C结构?
过去一小时我一直在寻找这个问题的答案,但找不到有效的解决方案.我正在尝试使用函数指针来调用特定对象的非静态成员函数.我的代码编译得很好,但在运行时我遇到了一个讨厌的运行时异常,它说:
运行时检查失败#0 - ESP的值未在函数调用中正确保存.这通常是调用使用一个调用约定声明的函数和使用不同调用约定声明的函数指针的结果.
很多网站都说在方法头中指定了调用约定,所以我__cdecl之前添加了它.但是,我的代码在更改后遇到了相同的运行时异常(我也尝试过使用其他调用约定).我不知道为什么我必须首先指定cdecl,因为我的项目设置被设置为cdecl.我正在使用一些外部库,但在添加此函数指针之前,这些工作正常.
我正在关注此:https://stackoverflow.com/a/151449
我的代码:
啊
#pragma once
class B;
typedef void (B::*ReceiverFunction)();
class A
{
public:
A();
~A();
void addEventListener(ReceiverFunction receiverFunction);
};
Run Code Online (Sandbox Code Playgroud)
A.cpp
#include "A.h"
A::A(){}
A::~A(){}
void A::addEventListener(ReceiverFunction receiverFunction)
{
//Do nothing
}
Run Code Online (Sandbox Code Playgroud)
BH
#pragma once
#include <iostream>
#include "A.h"
class B
{
public:
B();
~B();
void testFunction();
void setA(A* a);
void addEvent();
private:
A* a;
};
Run Code Online (Sandbox Code Playgroud)
B.cpp
#include "B.h"
B::B(){}
B::~B(){}
void B::setA(A* a)
{
this->a = a;
}
void …Run Code Online (Sandbox Code Playgroud) c++ member-function-pointers function-pointers visual-studio-2010
我之前从未使用过函数指针,而且在使代码工作时遇到了一些麻烦.这就是我所拥有的
TestClass.h:
class TestClass
{
public:
void function1();
void function2();
void function3(void (*funcPtr)(void))
void function4();
};
Run Code Online (Sandbox Code Playgroud)
TestClass.cpp
void TestClass::function1()
{
//CODE
}
void TestClass::function2()
{
//CODE
}
void TestClass::function3(void (*funcPtr)(void))
{
//CODE
funcPtr();
//CODE
}
void TestClass::function4()
{
function3(function1);
function3(function2);
}
Run Code Online (Sandbox Code Playgroud)
这给了我错误
"获取成员函数地址的非标准形式
我试图添加*funcPtr的TestClass :: infront,但这给了我更多的错误
我想要做的是有一个类包含第二个类的函数指针的映射,但第二个类的名称应该无关紧要(不能硬编码到第一个类)我真的希望能够实现这没有使用宏.我已经跟随了关于函数指针的learncpp.com中的示例,但是当在类之间传递它们时,我真的迷路了!我的尝试如下:
#include <map>
class Class1;
typedef double(Class1::*memFunc)();
class Class1
{
private:
std::map<std::string, memFunc> funcMap;
public:
void addFunc(std::string funcName, memFunc function)
{
funcMap.insert(std::pair<std::string, memFunc>(funcName, function));
}
};
class MyClass
{
public:
MyClass()
{
//How do I add member function getValue() to Class1?
class1.addFunc("new function", getValue());
}
double getValue()
{
return 0;
}
private:
Class1 class1;
};
Run Code Online (Sandbox Code Playgroud) 我需要一个将代码映射到C++成员函数的表.假设我们有这个类:
class foo
{
bool one() const;
bool two() const;
bool call(char*) const;
};
Run Code Online (Sandbox Code Playgroud)
我想要的是这样一个表:
{
{ “somestring”, one },
{ ”otherstring”, two }
};
Run Code Online (Sandbox Code Playgroud)
因此,如果我有一个foo对象f,f.call(”somestring”)将在表中查找"somestring",调用one()成员函数,并返回结果.
所有被调用的函数都有相同的原型,即它们是const,不带参数,返回bool.
这可能吗?怎么样?
AFAIK,在C++中,在同一个类的函数成员中调用另一个成员函数不应该需要"this"前缀,因为它是隐式的.但是,在使用函数指针的特定情况下,编译器需要它.仅当我通过func指针包含调用的"this"前缀时,以下代码才能正确编译 -
当使用函数指针时,编译器可以在它指向同一个类的成员函数时推断出它吗?
class FooBar
{
private:
int foo;
public:
FooBar()
{
foo = 100;
}
int GetDiff(int bar)
{
return abs(foo - bar);
}
typedef int(FooBar::*MyFuncPtr)(int);
void FooBar::Bar()
{
MyFuncPtr f = &FooBar::GetDiff;
(this->*f)(10);
GetDiff(10);
}
};
Run Code Online (Sandbox Code Playgroud) 我想在类中定义一个成员函数并使用它的指针.我知道我可以使用静态成员函数,但问题是我只能访问类的静态成员.有没有静态成员函数以外的方法可以获取函数指针.
更具体一点:我正在使用一个库,它将函数指针作为输入.我想编写一个成员函数并将其函数指针指向该外部库.我应该创建类的对象还是使用此指针来执行此操作?
c++ static member-function-pointers function-pointers functor
这是我试图实现的目标:
class MyClass
{
public:
template<typename T>
void whenEntering( const std::string& strState,
T& t,
void T::(*pMemberFunction)(void)) /// compilation fails here
{
t.(*pMemberFunction)(); // this line is only an example
}
}
Run Code Online (Sandbox Code Playgroud)
它是一种回调系统,用于对我收到的某些事件作出反应.
但是Visual 2010给出了以下编译错误:
error C2589: '(' : illegal token on right side of '::'
Run Code Online (Sandbox Code Playgroud)
我可能错误的指向成员的语法...但我也担心我可能不会这样定义模板...你有什么想法吗?
我正在尝试创建一个包含不同类的成员函数指针的映射.成员函数都具有相同的签名.为了做到这一点,我的所有类都继承了一个Object类,该类只有默认构造函数,虚拟析构函数和虚拟ToString()const方法.
// The map looks like this
map<Object*, void (Object::*)()> mapOfMethodPointers;
// And here are the two classes that inherit Object
// Class A
class A : public Object
{
public:
void AFunc();
string ToString() const override;
};
void A::AFunc()
{
cout << "AFunc()" << endl;
}
string A::ToString() const
{
cout << "A" << endl;
}
// Class B
class B : public Object
{
public:
void BFunc();
string ToString() const override;
}
void B::BFunc()
{
cout << "BFunc()" …Run Code Online (Sandbox Code Playgroud) 我想nth_element在一个类中使用我自己的排序函数(它应该可以访问对象的数据).目前,我正在做以下事情:
class Foo
{
public:
glm::vec3 *points;
int nmbPoints;
bool idxPointCompareX(int a, int b);
void bar();
}
bool Foo::idxPointCompareX(int a, int b)
{return points[a].x < points[b].x;)
void Foo::bar()
{
stl::vector<int> idxPointList;
for(int i = 0; i < nmbPoints; i++) idxPointList.push_back(i);
stl::nth_element(idxPointList.first(),idxPointList.first()+nmbPoints/2,idxPointList.end(), idxPointCompareX);
}
Run Code Online (Sandbox Code Playgroud)
当然,这不起作用,我得到错误:"必须调用非静态成员函数的引用".之后,我看了一下参考非静态成员函数必须调用,如何std::function用成员函数初始化?以及其他一些问题.我理解为什么这不起作用,但我不确定如何解决这个问题.
有人可以帮助我并告诉我如何解决这个问题吗?