0

Query on Smart pointer implementation

Hi All I have tried to implement shared and unique pointer on C++. Please find code below: https://code.sololearn.com/cmmh7hK7xx2b Is implementation of Unique pointer correct? Can you suggest implementation of shared pointer for reference count? I feel it is working ok now but I had to change copy constructor and assignment operator argument to non constant. Also please share some ideas on how can I pass argument to constructor of unique and shared pointer?

1st Nov 2021, 5:40 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
5 Respuestas
+ 4
"' ` `As I recall those points referred to source code you posted then, but haven't included now. The `->` point: that code provided only a non-`const` overload of `operator->`. And that overload, which potentially can modify the object, can't be called on a `const` object. So to support calling `->` on a `const` object you need to also or instead provide a `const` overload. The default-constructed point: your smart pointer constructor created the pointee object, and didn't pass any constructor arguments. You can pass constructor arguments via a template parameter pack, like template< class... Args > SmartPtr( Args&&... args ): p( new T( forward<Args>( args )... ) {} "
4th Nov 2021, 5:18 AM
A S Raghuvanshi
A S Raghuvanshi - avatar
+ 2
I think that's too much to delve into in a Solo comment thread, so I'll only discuss the `clsUniquePointer`. First, kudos on taking up the challenge to code that, and, not the least, to ask for critique. I'll try to be very constructive. So first, this class works for automating destruction of the pointed to object, which makes code more exception safe, and that's good. With C++17 and later it can also be returned from functions, because with C++17 and later even non-copyable objects can be returned, provided it's an object created by the function itself. Because then for a function call as initializer it's created right in the calling code's variable, and otherwise it's created right in the calling code's temporary object. That's Return Value Optimization in action. But then are the limitations: • `->` can not be applied to a `const clsUniquePointer`. • The pointed to object is always default-constructed, there's no way to pass constructor parameters.
2nd Nov 2021, 3:04 AM
A S Raghuvanshi
A S Raghuvanshi - avatar
+ 2
 A `clsUniquePointer` pointer can't be moved, e.g. `p2 = move( p1 );`. Moving is often desirable. Even necessary. • A `clsUniquePointer` pointer can't be reset, except via an explicit destructor invocation. Reset can expressed in terms of moving. But direct reset functionality can be more clear and slightly more efficient, so e.g. `std::unique_ptr` offers that. • A `clsUniquePointer` can't be released. That in turn means that it cannot be transferred to a shared ptr or to some other ownership. Which is sometimes necessary. It can be a good idea to devise test cases for these problems first, before doing any detailed analysis and coding. You can make sure that the test cases work with `std::unique_ptr` (and for ownership transfer, with `std::shared_ptr`). Then you know that when they instead use `clsUniquePtr`, then if one fails it's not the test case code's fault. Or, know or know, you can be pretty sure.
2nd Nov 2021, 3:06 AM
A S Raghuvanshi
A S Raghuvanshi - avatar
0
Appreciate your feedback. I got your point about implementing or at least think about core functionality standard smart pointer offers. I also got idea of return value optimization you talked about. But two things i could not understand and those are const and default constructed. If possible, could you please elaborate or share some hint on below two ? • `->` can not be applied to a `const clsUniquePointer`. • The pointed to object is always default-constructed, there's no way to pass constructor parameters.
3rd Nov 2021, 5:14 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
0
Thank you
4th Nov 2021, 5:26 AM
Ketan Lalcheta
Ketan Lalcheta - avatar