Skip to main content
Code Review

Return to Answer

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

This is not unique_ptr, it is auto_ptr, which has problems problems, mainly that copying a value modifies it, which is counter intuitive.

Assuming you really do not have move semantics and cannot do better (you should, C++11 is old now), there are still some problems:

  • There is no way to release a pointer. If I am not happy with your SmartPointer I want to do something like SmartPointer<T> sp; shared_ptr<T> ssp = sp.release();.

  • You forgot to return the stream inside ostream& operator <<. This should be a compilation error.

  • There is no need for friend. I don't know who made it popular to make the ostream& operator << a friend but I'd like to give that person a stern look. Without friend this works just fine:

     template <typename E>
     std::ostream& operator<<(std::ostream& stream, const SmartPointer<E>& smartPointer){
     return stream << &(*smartPointer);
     }
    
  • Your copy-constructor is wrong. It copies the pointer eventually resulting in a double delete. You probably meant to have other.pointer = nullptr;.

  • Consider making the assignment operator return void. You are supposed to return *this to allow chaining like in a = b = c = nullptr;. However, since the assignment takes the value away there is no point chaining assignments, so better cause a compiler error.

  • Your assignment operator causes a memory leak in the following case:

     SmartPointer<int> sp1 = new int{1};
     SmartPointer<int> sp2 = new int{2};
     sp1 = sp2;
    

The memory for the first allocation is never deleted.

Edit: There is a different memory leak case:

 SmartPointer<int> sp = new int;
 sp = sp;

I would suggest nullptr instead of NULL, but I guess your compiler does not know what that is.

This is not unique_ptr, it is auto_ptr, which has problems, mainly that copying a value modifies it, which is counter intuitive.

Assuming you really do not have move semantics and cannot do better (you should, C++11 is old now), there are still some problems:

  • There is no way to release a pointer. If I am not happy with your SmartPointer I want to do something like SmartPointer<T> sp; shared_ptr<T> ssp = sp.release();.

  • You forgot to return the stream inside ostream& operator <<. This should be a compilation error.

  • There is no need for friend. I don't know who made it popular to make the ostream& operator << a friend but I'd like to give that person a stern look. Without friend this works just fine:

     template <typename E>
     std::ostream& operator<<(std::ostream& stream, const SmartPointer<E>& smartPointer){
     return stream << &(*smartPointer);
     }
    
  • Your copy-constructor is wrong. It copies the pointer eventually resulting in a double delete. You probably meant to have other.pointer = nullptr;.

  • Consider making the assignment operator return void. You are supposed to return *this to allow chaining like in a = b = c = nullptr;. However, since the assignment takes the value away there is no point chaining assignments, so better cause a compiler error.

  • Your assignment operator causes a memory leak in the following case:

     SmartPointer<int> sp1 = new int{1};
     SmartPointer<int> sp2 = new int{2};
     sp1 = sp2;
    

The memory for the first allocation is never deleted.

Edit: There is a different memory leak case:

 SmartPointer<int> sp = new int;
 sp = sp;

I would suggest nullptr instead of NULL, but I guess your compiler does not know what that is.

This is not unique_ptr, it is auto_ptr, which has problems, mainly that copying a value modifies it, which is counter intuitive.

Assuming you really do not have move semantics and cannot do better (you should, C++11 is old now), there are still some problems:

  • There is no way to release a pointer. If I am not happy with your SmartPointer I want to do something like SmartPointer<T> sp; shared_ptr<T> ssp = sp.release();.

  • You forgot to return the stream inside ostream& operator <<. This should be a compilation error.

  • There is no need for friend. I don't know who made it popular to make the ostream& operator << a friend but I'd like to give that person a stern look. Without friend this works just fine:

     template <typename E>
     std::ostream& operator<<(std::ostream& stream, const SmartPointer<E>& smartPointer){
     return stream << &(*smartPointer);
     }
    
  • Your copy-constructor is wrong. It copies the pointer eventually resulting in a double delete. You probably meant to have other.pointer = nullptr;.

  • Consider making the assignment operator return void. You are supposed to return *this to allow chaining like in a = b = c = nullptr;. However, since the assignment takes the value away there is no point chaining assignments, so better cause a compiler error.

  • Your assignment operator causes a memory leak in the following case:

     SmartPointer<int> sp1 = new int{1};
     SmartPointer<int> sp2 = new int{2};
     sp1 = sp2;
    

The memory for the first allocation is never deleted.

Edit: There is a different memory leak case:

 SmartPointer<int> sp = new int;
 sp = sp;

I would suggest nullptr instead of NULL, but I guess your compiler does not know what that is.

Added link for release to auto_ptr::release
Source Link
nwp
  • 1.6k
  • 11
  • 14

This is not unique_ptr, it is auto_ptr, which has problems, mainly that copying a value modifies it, which is counter intuitive.

Assuming you really do not have move semantics and cannot do better (you should, C++11 is old now), there are still some problems:

  • There is no way to releaserelease a pointer. If I am not happy with your SmartPointer I want to do something like SmartPointer<T> sp; shared_ptr<T> ssp = sp.release();.

  • You forgot to return the stream inside ostream& operator <<. This should be a compilation error.

  • There is no need for friend. I don't know who made it popular to make the ostream& operator << a friend but I'd like to give that person a stern look. Without friend this works just fine:

     template <typename E>
     std::ostream& operator<<(std::ostream& stream, const SmartPointer<E>& smartPointer){
     return stream << &(*smartPointer);
     }
    
  • Your copy-constructor is wrong. It copies the pointer eventually resulting in a double delete. You probably meant to have other.pointer = nullptr;.

  • Consider making the assignment operator return void. You are supposed to return *this to allow chaining like in a = b = c = nullptr;. However, since the assignment takes the value away there is no point chaining assignments, so better cause a compiler error.

  • Your assignment operator causes a memory leak in the following case:

     SmartPointer<int> sp1 = new int{1};
     SmartPointer<int> sp2 = new int{2};
     sp1 = sp2;
    

The memory for the first allocation is never deleted.

Edit: There is a different memory leak case:

 SmartPointer<int> sp = new int;
 sp = sp;

I would suggest nullptr instead of NULL, but I guess your compiler does not know what that is.

This is not unique_ptr, it is auto_ptr, which has problems, mainly that copying a value modifies it, which is counter intuitive.

Assuming you really do not have move semantics and cannot do better (you should, C++11 is old now), there are still some problems:

  • There is no way to release a pointer. If I am not happy with your SmartPointer I want to do something like SmartPointer<T> sp; shared_ptr<T> ssp = sp.release();.

  • You forgot to return the stream inside ostream& operator <<. This should be a compilation error.

  • There is no need for friend. I don't know who made it popular to make the ostream& operator << a friend but I'd like to give that person a stern look. Without friend this works just fine:

     template <typename E>
     std::ostream& operator<<(std::ostream& stream, const SmartPointer<E>& smartPointer){
     return stream << &(*smartPointer);
     }
    
  • Your copy-constructor is wrong. It copies the pointer eventually resulting in a double delete. You probably meant to have other.pointer = nullptr;.

  • Consider making the assignment operator return void. You are supposed to return *this to allow chaining like in a = b = c = nullptr;. However, since the assignment takes the value away there is no point chaining assignments, so better cause a compiler error.

  • Your assignment operator causes a memory leak in the following case:

     SmartPointer<int> sp1 = new int{1};
     SmartPointer<int> sp2 = new int{2};
     sp1 = sp2;
    

The memory for the first allocation is never deleted.

Edit: There is a different memory leak case:

 SmartPointer<int> sp = new int;
 sp = sp;

I would suggest nullptr instead of NULL, but I guess your compiler does not know what that is.

This is not unique_ptr, it is auto_ptr, which has problems, mainly that copying a value modifies it, which is counter intuitive.

Assuming you really do not have move semantics and cannot do better (you should, C++11 is old now), there are still some problems:

  • There is no way to release a pointer. If I am not happy with your SmartPointer I want to do something like SmartPointer<T> sp; shared_ptr<T> ssp = sp.release();.

  • You forgot to return the stream inside ostream& operator <<. This should be a compilation error.

  • There is no need for friend. I don't know who made it popular to make the ostream& operator << a friend but I'd like to give that person a stern look. Without friend this works just fine:

     template <typename E>
     std::ostream& operator<<(std::ostream& stream, const SmartPointer<E>& smartPointer){
     return stream << &(*smartPointer);
     }
    
  • Your copy-constructor is wrong. It copies the pointer eventually resulting in a double delete. You probably meant to have other.pointer = nullptr;.

  • Consider making the assignment operator return void. You are supposed to return *this to allow chaining like in a = b = c = nullptr;. However, since the assignment takes the value away there is no point chaining assignments, so better cause a compiler error.

  • Your assignment operator causes a memory leak in the following case:

     SmartPointer<int> sp1 = new int{1};
     SmartPointer<int> sp2 = new int{2};
     sp1 = sp2;
    

The memory for the first allocation is never deleted.

Edit: There is a different memory leak case:

 SmartPointer<int> sp = new int;
 sp = sp;

I would suggest nullptr instead of NULL, but I guess your compiler does not know what that is.

Added additional test case
Source Link
nwp
  • 1.6k
  • 11
  • 14

This is not unique_ptr, it is auto_ptr, which has problems, mainly that copying a value modifies it, which is counter intuitive.

Assuming you really do not have move semantics and cannot do better (you should, C++11 is old now), there are still some problems:

  • There is no way to release a pointer. If I am not happy with your SmartPointer I want to do something like SmartPointer<T> sp; shared_ptr<T> ssp = sp.release();.

  • You forgot to return the stream inside ostream& operator <<. This should be a compilation error.

  • There is no need for friend. I don't know who made it popular to make the ostream& operator << a friend but I'd like to give that person a stern look. Without friend this works just fine:

     template <typename E>
     std::ostream& operator<<(std::ostream& stream, const SmartPointer<E>& smartPointer){
     return stream << &(*smartPointer);
     }
    
  • Your copy-constructor is wrong. It copies the pointer eventually resulting in a double delete. You probably meant to have other.pointer = nullptr;.

  • Consider making the assignment operator return void. You are supposed to return *this to allow chaining like in a = b = c = nullptr;. However, since the assignment takes the value away there is no point chaining assignments, so better cause a compiler error.

  • Your assignment operator causes a memory leak in the following case:

     SmartPointer<int> sp1 = new int{1};
     SmartPointer<int> sp2 = new int{2};
     sp1 = sp2;
    

The memory for the first allocation is never deleted.

Edit: There is a different memory leak case:

 SmartPointer<int> sp = new int;
 sp = sp;

I would suggest nullptr instead of NULL, but I guess your compiler does not know what that is.

This is not unique_ptr, it is auto_ptr, which has problems, mainly that copying a value modifies it, which is counter intuitive.

Assuming you really do not have move semantics and cannot do better (you should, C++11 is old now), there are still some problems:

  • There is no way to release a pointer. If I am not happy with your SmartPointer I want to do something like SmartPointer<T> sp; shared_ptr<T> ssp = sp.release();.

  • You forgot to return the stream inside ostream& operator <<. This should be a compilation error.

  • There is no need for friend. I don't know who made it popular to make the ostream& operator << a friend but I'd like to give that person a stern look. Without friend this works just fine:

     template <typename E>
     std::ostream& operator<<(std::ostream& stream, const SmartPointer<E>& smartPointer){
     return stream << &(*smartPointer);
     }
    
  • Your copy-constructor is wrong. It copies the pointer eventually resulting in a double delete. You probably meant to have other.pointer = nullptr;.

  • Consider making the assignment operator return void. You are supposed to return *this to allow chaining like in a = b = c = nullptr;. However, since the assignment takes the value away there is no point chaining assignments, so better cause a compiler error.

  • Your assignment operator causes a memory leak in the following case:

     SmartPointer<int> sp1 = new int{1};
     SmartPointer<int> sp2 = new int{2};
     sp1 = sp2;
    

The memory for the first allocation is never deleted.

I would suggest nullptr instead of NULL, but I guess your compiler does not know what that is.

This is not unique_ptr, it is auto_ptr, which has problems, mainly that copying a value modifies it, which is counter intuitive.

Assuming you really do not have move semantics and cannot do better (you should, C++11 is old now), there are still some problems:

  • There is no way to release a pointer. If I am not happy with your SmartPointer I want to do something like SmartPointer<T> sp; shared_ptr<T> ssp = sp.release();.

  • You forgot to return the stream inside ostream& operator <<. This should be a compilation error.

  • There is no need for friend. I don't know who made it popular to make the ostream& operator << a friend but I'd like to give that person a stern look. Without friend this works just fine:

     template <typename E>
     std::ostream& operator<<(std::ostream& stream, const SmartPointer<E>& smartPointer){
     return stream << &(*smartPointer);
     }
    
  • Your copy-constructor is wrong. It copies the pointer eventually resulting in a double delete. You probably meant to have other.pointer = nullptr;.

  • Consider making the assignment operator return void. You are supposed to return *this to allow chaining like in a = b = c = nullptr;. However, since the assignment takes the value away there is no point chaining assignments, so better cause a compiler error.

  • Your assignment operator causes a memory leak in the following case:

     SmartPointer<int> sp1 = new int{1};
     SmartPointer<int> sp2 = new int{2};
     sp1 = sp2;
    

The memory for the first allocation is never deleted.

Edit: There is a different memory leak case:

 SmartPointer<int> sp = new int;
 sp = sp;

I would suggest nullptr instead of NULL, but I guess your compiler does not know what that is.

Source Link
nwp
  • 1.6k
  • 11
  • 14
Loading
lang-cpp

AltStyle によって変換されたページ (->オリジナル) /