我对一个成员函数的shared_ptr的窃取有问题

问题描述 投票:0回答:1

我想把一个类的方法(functor的实际工作函数)做成一个shared_ptr。我想把这个指针传递给一个函数调用者。但是,我不能做到这一点。我的代码如下。

#include <iostream>
#include <memory>
#include <functional>

using namespace std;
using Func_t = typename std::function<void(int)>;

void function_caller(std::shared_ptr<Func_t> func, int the_other_num) {
  (*func.get())(the_other_num);
}

class increment{
public:
  // constructor
  increment(int n) : num{n} {}
  // destructor
  virtual ~increment() =default;
  //increment function:
  void operator()(int other_num) {
    cout << "the num should be : floor[" << num <<
        " * " << num + 1 << " / "<< num-1 << "]"<< std::endl;
    this->num = num * (num + 1) / (num - 1);
    cout << "the num is :" << num  << std::endl;
    cout << "the sum of num and the other number is :"
         << num + other_num << std::endl;
  }

protected:
  int num;
};

int main()
{
  increment incr(2);
  auto function_ptr{std::make_shared<Func_t>(&incr())};
  for (int i = 0; i < 5; ++i) {
    function_caller(function_ptr, i);
  }
  return 0;
}

我的编译失败了

cd ~/tmp/ && g++ member_function_caller.cc && ./a.out
member_function_caller.cc: In function ‘int main()’:
member_function_caller.cc:35:52: error: no match for call to ‘(increment) ()’
   35 |   auto function_ptr{std::make_shared<Func_t>(&incr())};
      |                                                    ^
member_function_caller.cc:19:8: note: candidate: ‘void increment::operator()(int)’
   19 |   void operator()(int other_num) {
      |        ^~~~~~~~
member_function_caller.cc:19:8: note:   candidate expects 1 argument, 0 provided

而当我把代码改成:

#include <iostream>
#include <memory>
#include <functional>

using namespace std;
using Func_t = typename std::function<void(int)>;

void function_caller(std::shared_ptr<Func_t> func, int the_other_num) {
  (*func.get())(the_other_num);
}

class increment{
public:
  // constructor
  increment(int n) : num{n} {}
  // destructor
  virtual ~increment() =default;
  //increment function:
  void operator()(int other_num) {
    cout << "the num should be : floor[" << num <<
        " * " << num + 1 << " / "<< num-1 << "]"<< std::endl;
    this->num = num * (num + 1) / (num - 1);
    cout << "the num is :" << num  << std::endl;
    cout << "the sum of num and the other number is :"
         << num + other_num << std::endl;
  }

protected:
  int num;
};

int main()
{
  increment incr(2);
  auto function_ptr{std::make_shared<Func_t>(&incr(int other_num))};
  for (int i = 0; i < 5; ++i) {
    function_caller(function_ptr, i);
  }
  return 0;
}

它失败了

cd ~/tmp/ && g++ member_function_caller.cc && ./a.out
member_function_caller.cc: In function ‘int main()’:
member_function_caller.cc:35:52: error: expected primary-expression before ‘int’
   35 |   auto function_ptr{std::make_shared<Func_t>(&incr(int other_num))};
      |                                                    ^~~

如果有人能帮我解决这个问题,我将非常感激。

此外,我需要将函数调用者的参数保留为一个叫做 std::function 为了能够保留我的API,我需要在我的代码的不同部分。

c++ methods shared-ptr functor std-function
1个回答
0
投票

这个版本和预期的一样。

#include <iostream>
#include <memory>
#include <functional>

using namespace std;
using Func_t = typename std::function<void(int)>;

void function_caller(int the_other_num, std::shared_ptr<Func_t> func = nullptr) {
  (*func.get())(the_other_num);
}

class increment{
public:
  // constructor
  increment(int n) : num{n} {}
  // destructor
  virtual ~increment() =default;
  //increment function:
  void operator()(int other_num) {
    cout << "the num should be : floor[" << num <<
        " * " << num + 1 << " / "<< num-1 << "]"<< std::endl;
    this->num = num * (num + 1) / (num - 1);
    cout << "the num is :" << num  << std::endl;
    cout << "the sum of num and the other number is :"
         << num + other_num << std::endl;
  }

protected:
  int num;
};

int main()
{
  increment incr(2);
  auto function_ptr{std::make_shared<Func_t>([&incr](int i) {incr(i);})};
  for (int i = 0; i < 5; ++i) {
    function_caller(i, function_ptr);
  }
  return 0;
}

而输出结果是:

the num should be : floor[2 * 3 / 1]
the num is :6
the sum of num and the other number is :6
the num should be : floor[6 * 7 / 5]
the num is :8
the sum of num and the other number is :9
the num should be : floor[8 * 9 / 7]
the num is :10
the sum of num and the other number is :12
the num should be : floor[10 * 11 / 9]
the num is :12
the sum of num and the other number is :15
the num should be : floor[12 * 13 / 11]
the num is :14
the sum of num and the other number is :18
© www.soinside.com 2019 - 2024. All rights reserved.