从基类调用派生类函数而不使用虚函数

发布于 2024-12-07 13:43:42 字数 959 浏览 0 评论 0原文

嗨,假设我有这样的代码

// base class                                                                                                                                                

    class A {

    public:

      int CallMyFct(PtrToFCT what){
        return what(10);
      }

    };

    class B : public A {
      int myInt;

    public:
      B():myInt(10){}

      int myFct1(int val){
        return myInt+val;
      }

      int myFct2(int val){
        return myInt*2+val;
      }


      void Call(void){
        int rez1=CallMyFct(&myFct1);
        if (rez1!=20)
          cout << "You are wrong" << endl;

        int rez2=CallMyFct(&myFct2);
        if (rez2!=30)
          cout << "You are wrong" << endl;
      }

    };

现在我需要从基类调用这些 MyFct1、MyFct2 等,但我不能使用虚函数。所以这有点像继承反转。我不知道这是否可能。您认为 mem_fun 或任何其他适配器函数可以在这里工作吗?

我实际上需要弄清楚 PtrToFCT 是什么以及如何在 CallMyFCT 中传递 myFct1。

谢谢

Hi suppose I have a code like this

// base class                                                                                                                                                

    class A {

    public:

      int CallMyFct(PtrToFCT what){
        return what(10);
      }

    };

    class B : public A {
      int myInt;

    public:
      B():myInt(10){}

      int myFct1(int val){
        return myInt+val;
      }

      int myFct2(int val){
        return myInt*2+val;
      }


      void Call(void){
        int rez1=CallMyFct(&myFct1);
        if (rez1!=20)
          cout << "You are wrong" << endl;

        int rez2=CallMyFct(&myFct2);
        if (rez2!=30)
          cout << "You are wrong" << endl;
      }

    };

Now I need to call these MyFct1, MyFct2, etc. from the base class, but I can not use virtual functions. So it is sorta like inversion of inheritance. I dont know if thats even possible. Do you think mem_fun or any other adapter function would work here.

I actually need to figure out what would be that PtrToFCT and how the would I pass myFct1 in the CallMyFCT.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

秋日私语 2024-12-14 13:43:42

您必须将要调用的函数定义为static,并提供附加参数以将它们传递给对象实例(而不是如果调用为this,它们将获得常规成员函数)。

You have to define the functions to be called as static, and provide additional parameter to pass to them to the object instance (instead of this that they would be getting if called as regular member functions).

顾忌 2024-12-14 13:43:42

您可以使用 [boost::function<>]1,在 C++2011 中也称为 std::function,源自 。您没有说明为什么不能使用虚拟功能。目前尚不清楚该方法是否比虚拟函数提供了性能增强,但还有其他原因不使用虚拟函数。

无论如何,这满足(我的解释)您从基类调用函数的要求,这些函数的行为根据实际的派生类而有所不同,而不使用虚函数。我并不完全清楚你所期望的用途的例子。如果这个机制不能满足您的需求,请明确需求。

#include <iostream> 
#include <boost/bind.hpp>
#include <boost/function.hpp>

class A{
protected:
    typedef boost::function<int (int)> CallbackFunc;
    // A knows it can call *something* with a particular signature
    // but what is called can be overridden by the derived class.
    CallbackFunc m_callbackFunc;
public:
    A()  
    {
    m_callbackFunc = boost::bind(&A::same,this,_1); 
    } 
    int same(int val) const { return val; }
    int simulateVirtual(int val) const { return m_callbackFunc(val); }
}; //end class A

class B : public A {
    int m_offset;
public:
    B(int offset) : m_offset(offset) {
        m_callbackFunc = boost::bind(&B::offset,this,_1);
    }
    int offset(int val) const { return m_offset + val; }
}; // end class B 

int main() {
    A* pA = new A;
    A* pB = new B(42);
    std::cout << "simulateVirtual(10) called on a 'real' A instance=" 
    << pA->simulateVirtual(10) << "\n";
    std::cout << "simulateVirtual(10) called via A* on a B instance="
    << pB->simulateVirtual(10) << "\n";
    delete pB; // in real life I would use shared_ptr<>
    delete pA;
    return 0;
} // end main

You can do what you like more cleanly with [boost::function<>]1, also known as std::function from <functional> in C++2011. You do not state why you cannot use virtual functions. It is not clear that this method offers a performance enhancement over virtual functions, but there are other reasons not to use virtual functions.

In any event, this meets (my interpretation of) your requirements for calling functions from the base class that behave differently depending on the actual derived class without use of virtual functions. Your example of desired use is not completely clear to me. If this mechanism does not meet your needs, please clarify the needs.

#include <iostream> 
#include <boost/bind.hpp>
#include <boost/function.hpp>

class A{
protected:
    typedef boost::function<int (int)> CallbackFunc;
    // A knows it can call *something* with a particular signature
    // but what is called can be overridden by the derived class.
    CallbackFunc m_callbackFunc;
public:
    A()  
    {
    m_callbackFunc = boost::bind(&A::same,this,_1); 
    } 
    int same(int val) const { return val; }
    int simulateVirtual(int val) const { return m_callbackFunc(val); }
}; //end class A

class B : public A {
    int m_offset;
public:
    B(int offset) : m_offset(offset) {
        m_callbackFunc = boost::bind(&B::offset,this,_1);
    }
    int offset(int val) const { return m_offset + val; }
}; // end class B 

int main() {
    A* pA = new A;
    A* pB = new B(42);
    std::cout << "simulateVirtual(10) called on a 'real' A instance=" 
    << pA->simulateVirtual(10) << "\n";
    std::cout << "simulateVirtual(10) called via A* on a B instance="
    << pB->simulateVirtual(10) << "\n";
    delete pB; // in real life I would use shared_ptr<>
    delete pA;
    return 0;
} // end main
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文