【问题标题】:Is there a way to wrap a function call in or overload a functions call?有没有办法将函数调用包装在函数调用中或重载函数调用?
【发布时间】:2021-04-22 04:39:01
【问题描述】:

我的目标是让一个类继承自 C++ 中的另一个类,并以相同的方式重载所有父类方法。

因此,当调用方法时,会运行一些代码,调用原始方法并在派生类重载方法中运行更多代码。

class Base
{
  Base() {}
  ~Base() {}

  void base_method()
  {
    // Does something.
  }
}


template<class T>
class ClassWrapper : public T
{
public:
  ClassWrapper(T base) : T( base ) {}
  ~ClassWrapper() {}

  void wrap_function()
  {
    // multithread block {
    // call base method within multithread block.
      this->base_method();
    // }
  }
}

int main()
{
  Base B;
  ClassWrapper<Base> C( B );

  C.base_method();

  return 0;
}

理想情况下,对基类一无所知,但它的所有方法都可以被覆盖。

我不确定这是否可能,但如果有任何建议会很棒!

【问题讨论】:

  • 为什么不简单地T().base_method()?或者您在寻找CRTP
  • 这不是简单地调用基本方法本身而没有在多线程环境中调用它所必需的代码吗?而且它是通用的,因此任何类的任何方法都可以以类似的方式包装
  • 哪个基类?
  • 我不太确定你在问什么。听起来您正在尝试创建一个类,该类根据模板参数确定要覆盖其父类型的哪个成员?
  • 看这里:stackoverflow.com/a/48408987/1463922 使用操作符-> 或许有可能

标签: c++ inheritance operator-overloading wrapper


【解决方案1】:

使用继承,您可以这样做:

class Base
{
  Base() {}
  virtual ~Base() {}

 virtual void base_method()
  {
    // Does something.
  }
};


class BaseWrapper : public Base
{
public:
  BaseWrapper(Base base) : Bas( base ) {}

  void base_method() override
  {
    // Some code ...
    Base::base_method();
    // Some code ...
  }
}

int main()
{
  Base B;
  BaseWrapper C( B );

  C.base_method();
}

【讨论】:

    【解决方案2】:

    通过 CRTP(Curiously Recurring Template Pattern)实现的静态多态性可能对您有益。 阅读有关 CRTP 的更多信息 herehere

    假设您有一个 Wrapper 类,例如:

    template <typename Impl>
    class Wrapper {
    public:
        Wrapper() {}
        ~Wrapper() {}
    
        void some_preparation() {
            std::cout << "Wrapper work!" << std::endl;
        }
    };
    

    然后你就有了你的实际课程:

    class MyFoo : public Wrapper<MyFoo> {
    public:
        MyFoo() {}
        ~MyFoo() {}
    
        void foo() {
            Wrapper::some_preparation();
            std::cout << "Derived work!" << std::endl;
        }
    };
    

    最终,您可以使用上述代码:

    MyFoo wrappedFoo;
    wrappedFoo.foo();
    

    结果是:

    Wrapper work!
    Derived work!
    

    【讨论】:

      【解决方案3】:

      Jarod 的回答非常适合您的问题。但是,我想添加一个更侧重于您选择的设计而不是实现的答案。

      虽然您说您希望“以相同的方式重载所有父类方法”,但您的目标(“调用原始方法并在派生类重载方法中运行更多代码”)表明有点不同。

      第一个可能表示继承,但第二个可能指向工厂抽象设计模式(组合优于继承):

      #include<iostream>
      
      class AbstractBar
      {
      public:
        virtual void bar_method() = 0;
      };
      
      class Bar1 : public AbstractBar
      {
      public:
        void bar_method()  { 
        std::cout << "Bar 1" << std::endl; 
        }
      };
      
      class Bar2 : public AbstractBar
      {
      public:
        void bar_method()  {
          std::cout << "Bar 2" << std::endl;
        }
      };
      
      class Foo
      {
      public:
        Foo(AbstractBar* bar_) : bar(bar_) { }
        void foo_method() { 
            bar->bar_method();
            std::cout << "Foo" << std::endl;
        }
      private:
        AbstractBar* bar;
      };
      
      
      
      int main() {
          Bar1 bar;
          Foo foo(&bar);
          foo.foo_method();
      }
      

      成为代码的output

      Bar 1
      Foo
      

      或简化版本(根据您的需要):

      #include<iostream>
      
      class Bar {
      public:
        void bar_method()  {
          std::cout << "Bar" << std::endl;
        }
      };
      
      class Foo {
      public:
        Foo(Bar* bar_) : bar(bar_) { }
        void foo_method() { 
            bar->bar_method();
            std::cout << "Foo" << std::endl;
        }
      private:
        Bar* bar;
      };
      
      int main() {
          Bar bar;
          Foo foo(&bar);
          foo.foo_method();
      }
      

      【讨论】:

        猜你喜欢
        • 2012-02-17
        • 1970-01-01
        • 2021-08-15
        • 2016-08-01
        • 2018-05-30
        • 1970-01-01
        • 1970-01-01
        • 2010-11-16
        • 1970-01-01
        相关资源
        最近更新 更多