C++ Correct way to pass std::unique_ptr object as function parameter reference


Mendes

I have an std::unique_ptr<T>object and a library function with T&parameters . This function will change the Tobject data.

What is a better way to pass std::unique_ptr<T>to this function ? Is the above code correct? Is there a better way?

#include <iostream>
#include <string>
#include <memory>

class Test {

   public: 
       std::string message = "Hi";
};

void doSomething(Test& item)
{
    item.message = "Bye";
}

int main()
{
     std::unique_ptr<Test> unique = std::unique_ptr<Test>(new Test());

     std::cout << unique->message << " John." << std::endl;

     doSomething(*unique.get());

     std::cout << unique->message << " John." << std::endl;
}
Dietmar Kühl

When dereferencing pointers, you should use standard library smart pointers just like raw pointers. That is, you can simply use

std::unique_ptr<Test> unique(new Test());
std::cout << unique->message << " John.\n";
doSomething(*unique);

I've included declarations to show simplified usage and an output to emphasize non-usestd::endl .

Related


Pass const unique_ptr reference as parameter

code void f1(unique_ptr<A[]>& upA){ //some work... //callee can mess up smart pointer many ways for caller upA.reset(); //some work... } void f2(const unique_ptr<A[]>& upA){ //some work... //compiler stops callee from ruining s

Pass const unique_ptr reference as parameter

code void f1(unique_ptr<A[]>& upA){ //some work... //callee can mess up smart pointer many ways for caller upA.reset(); //some work... } void f2(const unique_ptr<A[]>& upA){ //some work... //compiler stops callee from ruining s

Pass const unique_ptr reference as parameter

code void f1(unique_ptr<A[]>& upA){ //some work... //callee can mess up smart pointer many ways for caller upA.reset(); //some work... } void f2(const unique_ptr<A[]>& upA){ //some work... //compiler stops callee from ruining s

std::unique_ptr as out parameter of function

Praven struct cls{ ~cls(){std::cout<<"dtor\n";} }; void foo(cls** pp){ *pp = new cls; } int main() { cls* raw_ptr = 0; foo(&raw_ptr); std::unique_ptr<cls> u_ptr{raw_ptr}; } Is there a way to directly access raw_pointerthe unique_ptrp

std::unique_ptr as out parameter of function

Praven struct cls{ ~cls(){std::cout<<"dtor\n";} }; void foo(cls** pp){ *pp = new cls; } int main() { cls* raw_ptr = 0; foo(&raw_ptr); std::unique_ptr<cls> u_ptr{raw_ptr}; } Is there a way to directly access raw_pointerthe unique_ptrp

How to pass std::unique_ptr to function

username How to pass std::unique_ptra to the function? Lets say I have the following classes: class A { public: A(int val) { _val = val; } int GetVal() { return _val; } private: int _val; }; The following will not compile: void My

How to pass std::unique_ptr to function

User3690202: How to pass std::unique_ptra to the function? Lets say I have the following classes: class A { public: A(int val) { _val = val; } int GetVal() { return _val; } private: int _val; }; The following will not compile: voi

How to pass std::unique_ptr to function

User3690202: How to pass std::unique_ptra to the function? Lets say I have the following classes: class A { public: A(int val) { _val = val; } int GetVal() { return _val; } private: int _val; }; The following will not compile: voi

Pass unique_ptr to function object

InKodeWeTrust Having trouble compiling the following. I created a function object and tried to pass a unique pointer, but the compiler complained that I was trying to access private data in the unique_ptr. This happened in msvc 2012 v110. class Work { }; clas

Correct way to pass reference as parameter to class

username I'm trying to keep a reference to another object in a class, so I'm using pointers as members. What is the best way to pass a reference and what is the difference between them. class Sprite { public: Sprite(); void SetTexture(Texture * const&

Correct way to pass reference as parameter to class

username I'm trying to keep a reference to another object in a class, so I'm using pointers as members. What is the best way to pass a reference and what is the difference between them. class Sprite { public: Sprite(); void SetTexture(Texture * const&

Correct way to pass reference as parameter to class

username I'm trying to keep a reference to another object in a class, so I'm using pointers as members. What is the best way to pass a reference and what is the difference between them. class Sprite { public: Sprite(); void SetTexture(Texture * const&

cannot pass std::unique_ptr in std::function<>

Magic DM #include <functional> #include <memory> #include <string> #include <iostream> void foo(std::function<void()> f) { f(); } int main() { std::unique_ptr<int> handle = std::make_unique<int>(5); foo( [h = std::move(handle)]() mutable