xxxxxxxxxx
#include <iostream>
using namespace std;
int *getDouble(int* x){
int *result = new int ((*x) * 2); // save the result in the heap
return result; //return the address of the result
}
int main(){
int x=5;
int *ptr = getDouble(&x); //the pointer points to the result address
cout<<*ptr<<endl;
}
xxxxxxxxxx
#include <iostream>
using namespace std;
int getProduct(int* x){
*x *= 2;
return *x;
}
int main(){
int x=5;
cout<<getProduct(&x)<<endl;
}
xxxxxxxxxx
// To some free-function
struct func_wrap
{
using f_t = func_wrap(*)();
f_t func;
f_t operator()() const noexcept { return func; }
operator f_t() const noexcept { return func; }
};
using func_t = func_wrap(*)();
// Function to wrap
func_wrap foo() { return func_wrap{foo}; }
func_t bar() { return foo(); }
func_t buz() { return foo()(); }
// Some functor
struct function
{
function operator()() const noexcept
{
return function();
}
};
function foo(int) { return function(); }
function bar(int) { return function()(); }
function buz(int) { return function()()(); }