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
nullor0?in c++, definition of
null0, there aesthetic difference. prefer avoid macros, use0. problemnullpeople mistakenly believe different0and/or not integer. in pre-standard code,nullwas/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,nullptrkeyword.
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
nullimplementation-defined c++ null pointer constant in international standard (4.10). 192192) possible definitions include
0,0l, not(void*)0.
Comments
Post a Comment