简体   繁体   中英

C++ function that returns pointer to a function

Function add exists that takes 4 arguments. The first 3 arguments are references to const int, the fourth argument is bool. The function returns pointer for int.

Function 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. This function returns a pointer to a function that takes a reference to const int and returns pointer for int.

add_to is a pointer to function, which is returned from function make_add_to

Can anyone finish this? I'm not sure if make_add_to is correct though

#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. This function returns a pointer to a function that takes a reference to const int and returns pointer to int

it's a pointer to function which takes 3 arguments...
and ask the question instead of ordering us to work finish something

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM