C++ 访问回调数据

C++ access callback data

我正在尝试围绕 class 函数创建一个包装器。我的包装器的目的是测试输入、输出和执行整个程序中各种调用的操作顺序。我试图不对被调用者进行任何更改 class。附件是我正在尝试实现但无法弄清楚的示例。

Main.cpp

#include "func_warpper.h"
#include "func.h"

int main()
{
    func_wrapper fw
    fun func;
    int origValue = 5;
    fw.caller([&](int origValue) { func.f(origValue); }, origValue);
    int output = func.getResult().number;
    std::cout << " value outputed by function 2 : " << output << std::endl;
    // output
    // note that above line does give me the result I am looking for
    // however, I want to be able to get this inside the function of caller
    return 0;
}

func.h ....我希望这个不被修改

#ifndef FUN_H
#define FUN_H

class fun 
{
    public:
        struct result
        {
            int number;
        };
        fun();
        ~fun();
        void f(int value);
        struct result getResult(){return this->testResult;};
        private:
            struct result testResult;
};
#endif

func.cpp ....我希望这个不被修改

#include "func.h"


fun::fun(){
    this->testResult.number = 0;
    return;
}
fun::~fun(){
    return;
}
void fun::f(int value){
    int updateValue = value * 5;
    this->testResult.number = updateValue;
}

func_wrapper.h ....我可以修改这个直到奶牛回家,请按照推荐的修改去火腿:)

class func_wrapper
{
public:
    struct new_result
    {
        int new_number;
    };
    func_wrapper();
    ~func_wrapper();

    void caller(std::function<void(int)> clb, int val);
    struct new_result getNewResult() { return this->new_testResult; };
    private:
        struct new_result new_testResult;
};

#endif

func_wrapper.cpp ....和上面一样,我可以修改这个直到奶牛回家,请按照建议的修改去火腿:)

#include "func_wrapper.h"


func_wrapper::func_wrapper()
{
    //ctor
    this->new_testResult.new_number = 0;
    return;
}

func_wrapper::~func_wrapper()
{
    //dtor
}

void func_wrapper::caller(std::function<void(int)> clb, int val)
{
    std::cout << " value entered into function: " << val << std::endl;
    // clb(val); seems to call the function but does not store locally anything
    clb(val);
    clb;
    // clb; seems to store all the information locally however I seem unable to 
    // to reach the infromation: clb -> [functor] -> func -> testResult -> number
    // would like ...
    int output = clb ???  // the result of what gets filled from number struct
    // if I attempt to #include func.h
    // func func;
    // func.getResult().number; locally the answer is zero with or without delay
}

经过几天的搜索,我没有找到任何可以帮助解决这个问题的东西,包括关于堆栈溢出的足够相似的问题。任何帮助将不胜感激,谢谢。

因此,我的理解是,在 func_wrapper::caller 内,您希望能够访问回调内包装的 class。不幸的是,你这样做的方式是不可能的。没有(合法的)方法可以到达函数内部并访问其参数。

但是,如果您将操作分解成多个组成部分,您就可以随心所欲。你会想要一个更像这样的调用函数:

template <typename Type, typename Function>
void caller(Type&& functor, Function function, int val)
{
   std::cout << " value entered into function: " << val << std::endl;
    
   std::invoke(function, functor, val);

   std::cout << "value inside wrapper: " << functor.getResult().number << "\rn";
}

然后这样称呼它。

fw.caller(func, &fun::f, origValue);

https://godbolt.org/z/151YfEeoo

@JohnFilleau 曾提到要传递 class object 而不是 class 中的函数。以下是基于他提供的示例代码的解决方案,我修改以使用该示例。我意识到这个问题令人困惑,但要感谢 JohnFilleau 和 Taekahn 的讨论。

在main.cpp

int main()
{
    func_wrapper fw;
    fun func;
    int origValue = 5;
    fw.caller2(func, origValue);
return 0:
}

func_wrapper::caller2

    void func_wrapper::caller2(fun& fun, int val)
{
    std::cout << " value entered into function: " << val << std::endl;
    fun.f(val);
    int output = fun.getResult().number;
    std::cout << " did this work: " << output << std::endl;

}

在 header 我不得不添加

#include "func.h"

对header的改动如下

void caller2(fun& fun, int val);