What do the default values ​​for pointers in constructors mean?

I am trying to understand this code (it is taken from here ):

template <class T> class auto_ptr
{
    T* ptr;
public:
    explicit auto_ptr(T* p = 0) : ptr(p) {}
    ~auto_ptr()                 {delete ptr;}
    T& operator*()              {return *ptr;}
    T* operator->()             {return ptr;}
    // ...
};

I have a problem with understanding this line of code: explicit auto_ptr(T* p = 0) : ptr(p) {}.

As far as I understand, with this line we are trying to define a constructor with one type argument pointer-to-object-of-T-class. Then we have = 0. What is it? Is this the default value? But how 0can it be the default value for a pointer (the pointer should have addresses as values, not integer ones).

+5
source share
2 answers

Yes, the = 0default value. For a pointer argument, it matches = NULL.

To quote Stroustrup :

NULL 0?

++ NULL 0, . , 0. NULL , , 0 / . NULL / - , / . .

, nullptr; , ++ 11. nullptr .

( ):

4.10 [conv.ptr]

1 (5.19) prvalue , 0 prvalue std:: nullptr_t. ; . .

NULL :

18.2 [support.types]

3 NULL ++, (4.10). 192

192) 0 0L, (void*)0.

+10
explicit auto_ptr(T* p = 0) : ptr(p) {}

auto_ptr, T* p, , = 0. ptr : ptr(p) {}. explicit.

+3

All Articles