Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use std::unique_ptr on a struct?

The title says most of it, how do I do this? I've Googled around a bit and nothing has told me that it can't be done, but nothing has explained how to do it either.

Take this snippet of code here:

#include <cstdio>
#include <memory>

int main(void)
{
    struct a_struct
    {
        char first;
        int second;
        float third;
    };

    std::unique_ptr<a_struct> my_ptr(new a_struct);

    my_ptr.first = "A";
    my_ptr.second = 2;
    my_ptr.third = 3.00;

    printf("%c\n%i\n%f\n",my_ptr.first, my_ptr.second, my_ptr.third);

    return(0);
}

As the people who can answer this already know, this doesn't work, it doesn't even compile.

My question is how do I make something like this work?

The compilation error (using g++-7) looks like

baduniqueptr6.cpp: In function ‘int main()’:
baduniqueptr6.cpp:15:12: error: ‘class std::unique_ptr<main()::a_struct>’ has no member named ‘first’
     my_ptr.first = "A";
            ^~~~~
baduniqueptr6.cpp:16:12: error: ‘class std::unique_ptr<main()::a_struct>’ has no member named ‘second’
     my_ptr.second = 2;
            ^~~~~~
baduniqueptr6.cpp:17:12: error: ‘class std::unique_ptr<main()::a_struct>’ has no member named ‘third’
     my_ptr.third = 3.00;
            ^~~~~
baduniqueptr6.cpp:19:34: error: ‘class std::unique_ptr<main()::a_struct>’ has no member named ‘first’
     printf("%c\n%i\n%f\n",my_ptr.first, my_ptr.second, my_ptr.third);
                                  ^~~~~
baduniqueptr6.cpp:19:48: error: ‘class std::unique_ptr<main()::a_struct>’ has no member named ‘second’
     printf("%c\n%i\n%f\n",my_ptr.first, my_ptr.second, my_ptr.third);
                                                ^~~~~~
baduniqueptr6.cpp:19:63: error: ‘class std::unique_ptr<main()::a_struct>’ has no member named ‘third’
     printf("%c\n%i\n%f\n",my_ptr.first, my_ptr.second, my_ptr.third);
                                                               ^~~~~
like image 704
Mutaru Avatar asked Aug 02 '19 02:08

Mutaru


People also ask

How do I pass a unique_ptr argument to a constructor or a function?

You cannot copy a unique_ptr . You can only move it. The proper way to do this is with the std::move standard library function. If you take a unique_ptr by value, you can move from it freely.

What is the use of std :: unique_ptr?

std::unique_ptr is a smart pointer that owns and manages another object through a pointer and disposes of that object when the unique_ptr goes out of scope. The object is disposed of, using the associated deleter when either of the following happens: the managing unique_ptr object is destroyed.

How is unique_ptr implemented in C++?

unique_ptr allows only one owner of the underlying pointer while shared_ptr is a reference-counted smart pointer. In this implementation, the developer doesn't need to explicitly delete the allocated memory towards the end of the function.

Can I assign unique_ptr?

It can be assigned: class owner { std::unique_ptr<someObject> owned; public: owner() { owned=std::unique_ptr<someObject>(new someObject()); } };


1 Answers

You should use -> instead of ., std::unique_ptr is a smart pointer which behaves similarly as raw pointers.

my_ptr->first = 'A';
my_ptr->second = 2;
my_ptr->third = 3.00;

printf("%c\n%i\n%f\n",my_ptr->first, my_ptr->second, my_ptr->third);

LIVE

Or you can use operator* to dereference on the pointer and then you can use operator., this is also the same as raw pointers.

(*my_ptr).first = 'A';
(*my_ptr).second = 2;
(*my_ptr).third = 3.00;

printf("%c\n%i\n%f\n",(*my_ptr).first, (*my_ptr).second, (*my_ptr).third);

LIVE

PS: You should change "A" (which is a c-style string) to 'A' (which is a char).

like image 92
songyuanyao Avatar answered Sep 18 '22 13:09

songyuanyao