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);
^~~~~
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.
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.
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.
It can be assigned: class owner { std::unique_ptr<someObject> owned; public: owner() { owned=std::unique_ptr<someObject>(new someObject()); } };
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
).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With