[英]C++ function that returns pointer to a function
Function add exists that takes 4 arguments.函数 add 存在,需要 4 个参数。 The first 3 arguments are references to const int, the fourth argument is bool.
前 3 个参数是对 const int 的引用,第四个参数是 bool。 The function returns pointer for int.
该函数返回 int 的指针。
Function make_add_to takes 3 arguments.函数 make_add_to 接受 3 个参数。 First is a pointer to function with the same structure as Add function, the second argument is a reference to const int, third is bool.
第一个是指向与 Add 函数结构相同的函数的指针,第二个参数是对 const int 的引用,第三个是 bool。 This function returns a pointer to a function that takes a reference to const int and returns pointer for int.
此函数返回一个指向函数的指针,该函数引用 const int 并返回 int 的指针。
add_to is a pointer to function, which is returned from function make_add_to add_to 是一个指向函数的指针,它是从函数 make_add_to 返回的
Can anyone finish this?有人能完成这个吗? I'm not sure if make_add_to is correct though
我不确定 make_add_to 是否正确
#include <iostream>
using namespace std;
int * add(const int &x1, const int &x2, const int &x3, bool y);
int * (*make_add_to(const int &x5))(int *(*f)(const int &x1, const int &x2, const int &x3, bool y),const int &x4, bool y);
int main()
{
int x=3,y=2,z=1;
//cout << *dodaj(x,y,z,true);
int *(*ptr)(const int &x1, const int &x2, const int &x3, bool y) = &add;
//cout << *ptr(x,y,z,true);
// (*dodaj_do)
}
int * add(const int &x1, const int &x2, const int &x3, bool y)
{
int * re = new int;
int x = x1+x2+x3;
re = &x;
return re;
}
This should work:这应该有效:
#include <iostream>
using namespace std;
int* add(const int &x1, const int &x2, const int &x3, bool y);
int* (*make_add_to(int*(const int&, const int&, const int&, bool),const int &x4, bool y))(const int&);
int main()
{
int x=3,y=2,z=1;
auto res_add = add(x,y,z,true);
cout << *res_add;
int* (*ptr)(const int &x1, const int &x2, const int &x3, bool y) = &add;
auto res_add_ptr = ptr(x,y,z,true);
cout << *res_add_ptr;
/* might leak memory before reaching here */
delete res_add;
delete res_add_ptr;
}
int * add(const int &x1, const int &x2, const int &x3, bool y)
{
int *re = new int{0};
*re += x1+x2+x3;
return re;
}
but you should use something like this但你应该使用这样的东西
#include <iostream>
#include <memory>
std::unique_ptr<int> add(const int &x1, const int &x2, const int &x3, bool y);
auto make_add_to(std::unique_ptr<int>(const int&, const int&, const int&, bool),const int &x4, bool y);
int main()
{
int x=3,y=2,z=1;
std::cout << *add(x,y,z,true);
auto ptr = &add;
std::cout << *ptr(x,y,z,true);
}
std::unique_ptr<int> add(const int &x1, const int &x2, const int &x3, bool y)
{
auto re = std::make_unique<int>(0);
*re += x1+x2+x3;
return re;
}
Function功能
int * (*make_add_to(const int &x5))(int *(*f)(const int &x1, const int &x2, const int &x3, bool y),const int &x4, bool y);
is not used, and is not没有使用,也没有
make_add_to takes 3 arguments First is a pointer to function with the same structure as Add function, the second argument is a reference to const int, third is bool. make_add_to 接受 3 个参数第一个是指向与 Add 函数结构相同的函数的指针,第二个参数是对 const int 的引用,第三个是 bool。 This function returns a pointer to a function that takes a reference to const int and returns pointer to int
此函数返回一个指向函数的指针,该函数引用 const int 并返回指向 int 的指针
it's a pointer to function which takes 3 arguments...它是一个指向函数的指针,它带有 3 个参数......
and ask the question instead of ordering us to work finish something并提出问题而不是命令我们完成某事
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.