C++:将指向一个对象的成员函数的指针存储在另一个对象中
C++: Store pointer to a member function of an object in another object
我有一个类,它在某些情况下会调用用户指定的函数。因此,该类具有可用于"注册"函数的方法void setExternalPostPaintFunction(void(*function)(QPainter&));
。然后,将在那个场合调用此函数:
class A {
public:
void setExternalPostPaintFunction(void(*function)(QPainter&));
private:
void (*_externalPostPaint)(QPainter&);
bool _externalPostPaintFunctionAssigned;
};
函数指针保存在成员变量 _externalPostPaint
中。setExternalPostPaintFunction
的实现如下所示:
void A::setExternalPostPaintFunction(void(*function)(QPainter&)) {
_externalPostPaint = function;
_externalPostPaintFunctionAssigned = true;
}
现在,这适用于正常功能。但是,我希望还能够传递指向对象成员函数的指针。据我所知,在这种情况下,我还必须传递并存储指向对象的指针。但是,我不知道另一个对象将具有哪种类型。所以我想我被迫使用模板。我已经想到了这样的事情:
class A {
public:
template <typename T>
void setExternalPostPaintFunction(void(T::*function)(QPainter&), T* object);
private:
void (T::*_externalPostPaint)(QPainter&); //<- This can't work!
bool _externalPostPaintFunctionAssigned;
};
这样,我可以将函数指针和对象指针传递给setExternalPostPaintFunction
并且可能能够在该函数内的对象上调用该函数。但是我无法将其存储在变量_externalPostPaint
中,因为只有在调用函数setExternalPostPaintFunction
时才会推断出T
类型,因此我不能拥有依赖于此类型的成员变量,因为我的成员变量的类型必须在创建对象时知道,除此之外它不能更改, 但是,在分配一个新函数的情况下,它必须是不同类型的对象的成员函数。
那么正确的方法是什么,或者有没有?我不太适合模板和函数指针,所以我可能忽略了一些东西。
Anoter 选项肯定是创建一个具有虚拟成员函数的函子类,该函数可以在派生类中覆盖,然后传递 + 存储该类型的对象指针而不是函数指针。但如果可能的话,我不知何故更喜欢我的方法。
编辑:解决方案
TartanLlama通过建议使用std::function
使我走上了正确的轨道。这是我解决它的方法:
class A {
public:
template <typename T>
void setExternalPostPaintFunction(T* object, void(T::*function)(QPainter&)) {
_externalPostPaint = std::bind(function, object, std::placeholders::_1);
_externalPostPaintFunctionAssigned = true;
}
void setExternalPostPaintFunction(std::function<void(QPainter&)> const& function);
private:
std::function<void(QPainter&)> _externalPostPaint;
bool _externalPostPaintFunctionAssigned;
};
如您所见,指向函数/成员函数的指针现在存储在 std::function<void(QPainter&)>
对象中。优点是,std::function
基本上可以存储任何可调用的目标。然后有两个重载:一个可用于任何也接受例如普通函数指针的std::function
对象(因为预期的std::function
是从该指针隐式构造的),另一个用于必须在对象上调用的成员函数(更多是为了方便)。后者作为模板实现。这使用 std::bind
在对象(用户传递)上创建该成员函数(用户传递)调用的std::function
对象。
采用std::function
的重载在源文件中实现,如下所示:
void ImageView::setExternalPostPaintFunction(std::function<void(QPainter&)> const& function) {
_externalPostPaint = function;
_externalPostPaintFunctionAssigned = true;
}
在类 A
的代码中调用该存储函数现在非常简单:
//canvas is a QPainter instance
if (_externalPostPaintFunctionAssigned) _externalPostPaint(canvas);
想要将成员函数注册为回调函数的用户只需执行以下操作:
//_imageView is an instance of "A"
//"MainInterface" is the type of "this"
_imageView->setExternalPostPaintFunction(this, &MainInterface::infoPaintFunction);
或者,如果它不是成员函数,而只是一个普通函数:
void someFunction(QPainter& painter) {
//do stuff
}
_imageView->setExternalPostPaintFunction(&someFunction);
或者他可以显式创建一个std::function
对象并传递它:
std::function<void(QPainter&)> function = [&](QPainter& painter){ this->infoPaintFunction(painter); };
_imageView->setExternalPostPaintFunction(function);
就像一个魅力。
std::function
:
class A {
public:
//PostPaintFun can be anything which acts as a function taking a QPainter&
//Could be a lambda, function pointer, functor, etc.
using PostPaintFun = std::function<void(QPainter&)>;
void setExternalPostPaintFunction(PostPaintFun fun);
private:
//Names beginning with an underscore are reserved, don't use them
//Ending with an underscore is fine
PostPaintFun fun_;
bool externalPostPaintFunctionAssigned_;
};
现在,您可以使用如下成员函数:
struct B
{
void exec(QPainter&) const;
};
void foo() {
B b;
a.setExternalPostPaintFunction(
[b] (QPainter& p) {b.exec(p);}
);
}
//or inside B
void B::foo() {
a.setExternalPostPaintFunction(
[this] (QPainter&p) {this->exec(p);}
);
}
我不得不说我更喜欢TartanLlama的答案,但在这里你有一些可以为你工作的东西。
这可能需要一些工作,但我相信你会明白的。
struct IFunctionHolder {}; // Used for pointing to any FunctionHolder
typedef IFunctionHolder* functionHolder_ptr; // Alias for IFunctionHolder* .
template<typename Function> // The template for the actual function holders.
struct FunctionHolder: public IFunctionHolder
{
Function function;
};
class A {
public:
template <typename T>
void setExternalPostPaintFunction(void(T::*function)(QPainter&), T* object);
private:
functionHolder_ptr *function_holder; // This memeber can hold eny instantiation of template<> FunctionHolder.
// Instantiate this member wen calling setExternalPostPaintFunction
bool _externalPostPaintFunctionAssigned;
};
你可以有一些这样的代码:
A some_a;
void some_a.setExternalPostPaintFunction(&SomeInstance::some_fnunction); // Here take place the instantiation of FunctionHolder.
some_a.function_holder.function(some_painter);
- 为什么我不能将一个对象push_back到属于另一个类的对象向量中?
- 在他自己的方法中,有可能将一个对象取消引用到另一个对象吗
- 从多个源构造一个对象,包括一个对象向量
- 为什么C++在将一个对象复制到另一个对象时需要对这两个对象进行低级常量限定
- 检查哪个对象调用了另一个对象的对象方法
- C++是否有一个容器,每个类型最多存储一个对象
- 一个对象什么时候可以有非零大小的一个,但不能同时具有一个或多个字节的存储
- 使用C 中字符串标头的getline()函数将第一个char存储在字符串对象中
- 如何在C++中将多个子类的对象存储在一个向量中
- 在c++中存储一个对象或不存储对象的首选方式是什么
- 如何将具有不同类(但相同基础)的对象存储在一个变量中
- 作为一个对象存储在内存中意味着什么
- 我们可以把一个对象存储在一个文件中以便以后检索吗
- 将一个对象的成员函数作为回调函数存储在另一个对象中
- c++将一个对象存储到该对象的构造函数内的对象数组中
- 当一个对象被保证比其包含的对象更长寿时,应该如何存储它
- 创建一个新对象并将该对象存储在新类的向量中,属性消失
- 在JNI jobject中存储一个c++对象实例,稍后检索
- C++:将指向一个对象的成员函数的指针存储在另一个对象中
- 如何在c++中将对象存储在另一个对象中