Is it ok to dereference a shared pointer, assign and assign a new object to it like so:
void foo()
{
std::shared_ptr<std::string> x =
std::make_shared<std::string>();
bar(*x); // is this fine?
// x == bsl::string("WHATEVER")
}
void bar(string& y)
{
y = string("whatever");
}
What does “dereferencing” a pointer mean in C/C++? Dereferencing is used to access or manipulate data contained in memory location pointed to by a pointer. * (asterisk) is used with pointer variable when dereferencing the pointer variable, it refers to variable being pointed, so this is called dereferencing of pointers.
A shared pointer supports usual pointer dereferencing The shared pointer is, in fact, a class which has a raw pointer pointing to the managed object. This pointer is called stored pointer. We can access it
C C++ Server Side Programming Programming Dereferencing is used to access or manipulate data contained in memory location pointed to by a pointer. *(asterisk) is used with pointer variable when dereferencing the pointer variable, it refers to variable being pointed, so this is called dereferencing of pointers.
Therefore, the memory that a shared pointer takes is more than a raw pointer and a unique pointer. So, if a vector of a million pointers should be created, probably unique pointers are a better choice.
Yes, this is valid. Operator *
returns the result of dereferencing the stored (raw) pointer.
Dereferencing a (raw) pointer does not make a copy or return a temporary: dereferencing a pointer when passing by reference
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