c++ - What do default values of pointers in constructors mean? -


i try understand code (it taken 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 problem understanding line of code: explicit auto_ptr(t* p = 0) : ptr(p) {}.

as far understand, line try define constructor has 1 argument of pointer-to-object-of-t-class type. have = 0. that? default value? how 0 can default value of pointer (pointer should have addresses values, not integer).

yes, = 0 default value. pointer argument, same = null.

to quote stroustrup:

should use null or 0?

in c++, definition of null 0, there aesthetic difference. prefer avoid macros, use 0. problem null people mistakenly believe different 0 and/or not integer. in pre-standard code, null was/is defined unsuitable , therefore had/has avoided. that's less common these days.

if have name null pointer, call nullptr; that's it's called in c++11. then, nullptr keyword.

the formal definition of null pointer constant follows (emphasis mine):

4.10 pointer conversions [conv.ptr]

1 a null pointer constant integral constant expression (5.19) prvalue of integer type evaluates zero or prvalue of type std::nullptr_t. null pointer constant can converted pointer type; result null pointer value of type , distinguishable every other value of pointer object or pointer function type. such conversion called null pointer conversion.

null defined 1 such constant:

18.2 types [support.types]

3 macro null implementation-defined c++ null pointer constant in international standard (4.10). 192

192) possible definitions include 0 , 0l, not (void*)0.


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -