#include <iostream>
template<typename T>
class Pointer {
T *pointer;
public:
Pointer(T *pointer) : pointer(pointer) {}
virtual ~Pointer() {
delete pointer;
pointer = nullptr;
std::cout << "Pointer is deleted \n";
}
T *get() const {
return pointer;
}
};
Pointer<int> getPointer() {
return {newint{5}};
}
int main() {
auto value = *getPointer().get();
std::cout << value;
return 0;
}
as I understand in this line "*getPointer().get();" pointer object is immediately deleted as I don't assign it to local variable, that invokes Pointer's destructor, therefore corrupts original data.
Does it mean that I cannot use chain of function calls in the statement?
as I understand in this line "*getPointer().get();" pointer object is immediately deleted as I don't assign it to local variable, that invokes Pointer's destructor, therefore corrupts original data.
You're forgetting that value is a copy of the referenced object. The original object is destroyed, but the copy lives on.
If instead you cached a raw pointer (i.e., you didn't transfer ownership through Pointer): int *value = getPointer().get(); // note: no dereferencing operator
Then *value's lifetime would end at the semicolon, but this isn't what happens in the code you posted.
Basically I have to be very careful with Object handles and pay attention to temporary scope so I don't lose objects that I don't want to de-reference immediately in the same statement.
This problematic code int *p = getPointer().get(); // note: no dereferencing operator
Fails to properly transfer unique ownership from the temporary unique_ptr. p becomes a dangling pointer on the next line.
The member function get() exists for the express purpose of avoiding the ownership transfer. Calling it on an rvalue (which might soon be destroyed) is potentially a mistake. If you really are worried about this, you can forbid it if you want, but realize this is not always wrong:
The correct way to write the above is unique_ptr<int> p = getPointer();
Note that no copy of the resource is made here: p now owns the resource pointed to by the result of getPointer().