C ++指向UNKNOWN类的成员函数的指针

编程入门 行业动态 更新时间:2024-10-27 00:33:30
本文介绍了C ++指向UNKNOWN类的成员函数的指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

免责声明我不要使用 BOOST 或其他图书馆

ve了解如何 PointerToMemberFunction 工作。这是我的示例代码。

#include< iostream> using namespace std; class Foo { public: void foo() { cout< 我是一个foo方法\\\; }; }; class Bar { public: void bar(Foo * fooPtr,void(Foo :: * fooFnPtr)()) {(fooPtr-> * fooFnPtr)(); }; }; int main() { Foo * foo = new Foo(); Bar * bar = new Bar(); bar-> bar(foo,& Foo :: foo); return 0; }

现在,问题是什么。 Bar :: bar 必须以某种方式修改,因为在实际项目中不会知道什么类 fooFnPtr 是指向的指针。换句话说, Bar :: bar 必须使用任何类,不仅与 Foo 。我不知道,指向传递给 Bar :: bar 的类的实例的指针。

一个可以帮助的事情是,所有使用 Bar :: bar 的类都是一个类的子

<这是可以实现的吗?如何修复我的代码?提前感谢!

解决方案

您可以使 bar

模板< class T> void bar(T * fooPtr,void(T :: * fooFnPtr)()) {(fooPtr-> * fooFnPtr) }

当然,如果你只想传递指针到共同基类,你可以这样做:

#include< iostream> using namespace std; class Foo { public: virtual void foo() { cout< 我是一个foo方法\\\; }; }; class Derived:public Foo { public: virtual void foo() { cout< 我是一个派生方法\\\; }; }; class Bar { public: void bar(Foo * fooPtr,void(Foo :: * fooFnPtr)()) {(fooPtr-> * fooFnPtr)(); } }; int main() { Derived * derived = new Derived(); Bar * bar = new Bar(); bar-> bar(派生,& Foo :: foo); return 0; }

DISCLAIMER I DO NOT USE BOOST OR OTHER LIBRARIES

Finally I've learned how PointerToMemberFunction works. This is my example code.

#include <iostream> using namespace std; class Foo { public: void foo ( ) { cout << "I'm a foo method\n"; }; }; class Bar { public: void bar ( Foo* fooPtr , void(Foo::*fooFnPtr)() ) { (fooPtr->*fooFnPtr)(); }; }; int main() { Foo* foo = new Foo(); Bar* bar = new Bar(); bar->bar ( foo , &Foo::foo ); return 0; }

Now, what the problem is. Bar::bar must be modified somehow, because in real project it won't know, what class fooFnPtr is a pointer to. In other words Bar::bar must work with any class, not only with Foo. I won't know, a pointer to an instance of what class is passed to Bar::bar.

The one thing which can help is that all classes which will work with Bar::bar are children of one class!

Is this achievable and how? How do i fix my code? Thanks in advance!

解决方案

You could make bar a template function:

template<class T> void bar ( T* fooPtr , void(T::*fooFnPtr)() ) { (fooPtr->*fooFnPtr)(); }

Of course, if you only want to pass pointers to members that exist in the common base class, you can simply do this:

#include <iostream> using namespace std; class Foo { public: virtual void foo ( ) { cout << "I'm a foo method\n"; }; }; class Derived: public Foo { public: virtual void foo ( ) { cout << "I'm a Derived method\n"; }; }; class Bar { public: void bar ( Foo* fooPtr , void(Foo::*fooFnPtr)() ) { (fooPtr->*fooFnPtr)(); } }; int main() { Derived* derived = new Derived(); Bar* bar = new Bar(); bar->bar ( derived , &Foo::foo ); return 0; }

更多推荐

C ++指向UNKNOWN类的成员函数的指针

本文发布于:2023-07-28 02:45:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1226578.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:指针   函数   成员   UNKNOWN

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!