Search
 
SCRIPT & CODE EXAMPLE
 

CPP

pointer in return function c++

#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;
}
Comment

pointer in return function c++

#include <iostream>

using namespace std;

int getProduct(int* x){
    *x *= 2;
    return *x;   
}

int main(){
    int x=5;
    cout<<getProduct(&x)<<endl;
}
Comment

c++ function return pointer to itself

// 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()()(); }
Comment

PREVIOUS NEXT
Code Example
Cpp :: how to square a number in c++ 
Cpp :: reverse function in cpp array 
Cpp :: c++ progress bar 
Cpp :: unique_ptr syntax 
Cpp :: char to integer c++ 
Cpp :: classes and objects in c++ 
Cpp :: array to string c++ 
Cpp :: new float array c++ 
Cpp :: erase element from vector c++ 
Cpp :: gettimeofday header file 
Cpp :: c ifdef 
Cpp :: how to initialize vector 
Cpp :: opengl draw semi circle c++ 
Cpp :: slice a vector c++ 
Cpp :: check if element in dict c++ 
Cpp :: find in unordered_map c++ 
Cpp :: joining two vectors in c++ 
Cpp :: c++ for loop multiple variables 
Cpp :: getline() 
Cpp :: c++ average vector 
Cpp :: how to add space in c++ 
Cpp :: c++ constructor call superclass 
Cpp :: how to sort array in c++ stockoverflow 
Cpp :: trie code cpp 
Cpp :: QVariant to int 
Cpp :: hierarchical inheritance in c++ employee 
Cpp :: how to fill vector from inputs c++ 
Cpp :: input c++ 
Cpp :: c++ insert hashmap 
Cpp :: how to find factorial of number in c++ 
ADD CONTENT
Topic
Content
Source link
Name
8+8 =