Reputation: 35
I've been reading the section on smart pointers in Scott Meyer's "Effective C++" and "More Effective C++." In it, there is an implementation of auto_ptr
for which the constructor is written as follows:
template<class T>
class auto_ptr{
private:
T* pointee;
public:
auto_ptr<T>(T* p) : pointee(p) {}
// ... rest of class
My question is: why is the pointer in the constructor not passed by const reference? p
is not changed in the constructor, so const
seems fine, and passing by reference we avoid any copying. No explanation for this is given.
I also know that auto_ptr
has been deprecated, but a) the question still stands, and b) I think one sees the same thing with eg std::shared_ptr
Upvotes: 0
Views: 95
Reputation: 238441
Important notice: std::auto_ptr
has been deprecated in C++11 and has been removed from the language in C++17. It should not be used in new code.
why is the pointer in the constructor not passed by const reference?
Because pointers to objects are small objects and the cost of copying the reference is equally costly, and the added indirection is potentially even more costly compared to copying the pointer.
Upvotes: 0