-7

How can I print a value of a reference to a pointer?

int& createInt(){
    auto uPtr = make_unique<int>(2);
    auto ptr(uPtr.get() );
    return *ptr;
}

How would I print value 2 when I call createInt() function?

int& x = createInt();  // what will be the value of "x"
gath
  • 24,504
  • 36
  • 94
  • 124

1 Answers1

0

This won't work because a unique_ptr will automatically free it's memory when it goes out of scope.

http://en.cppreference.com/w/cpp/memory/unique_ptr

what you can do is

int createInt(){
    return 2;
}

or

int* createIntPtr(){
    return new int(2);
}

though that is probably bad practice. There are other things you can do like passing a reference to a value and editing it, depending on what you want though.

Gage Ervin
  • 105
  • 1
  • 8