I try 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 problem with understanding this line of the code: explicit auto_ptr(T* p = 0) : ptr(p) {}
.
As far as I understand, with this line we try to define a constructor that has one argument of pointer-to-object-of-T-class
type. Then we have = 0
. What is that? Is it a default value? But how 0
can be a default value of a pointer (pointer should have addresses as a values, not integer).
Pointers have no default value. The value they have is just whatever junk was in the memory they're using now. Sometimes a specific compiler will zero out memory, but that's not standard so don't count on it.)
A default constructor is a constructor that either has no parameters, or if it has parameters, all the parameters have default values. If no user-defined constructor exists for a class A and one is needed, the compiler implicitly declares a default parameterless constructor A::A() .
Default constructors are very useful to crate objects without having specific initial value. It is also used to create array of objects. Significance of Default constructors. Default constructors are very useful to crate objects without having specific initial value. It is also used to create array of objects.
For the sake of sanity, always initialize pointers to NULL, in that way if any attempt to dereference it without malloc or new will clue the programmer into the reason why the program mis-behaved.
Yes, = 0
is the default value. For a pointer argument, it is the same as = NULL
.
To quote Stroustrup:
Should I use
NULL
or0
?In C++, the definition of
NULL
is0
, so there is only an aesthetic difference. I prefer to avoid macros, so I use0
. Another problem withNULL
is that people sometimes mistakenly believe that it is different from0
and/or not an integer. In pre-standard code,NULL
was/is sometimes defined to something unsuitable and therefore had/has to be avoided. That's less common these days.If you have to name the null pointer, call it
nullptr
; that's what it's called in C++11. Then,nullptr
will be a keyword.
The formal definition of a null pointer constant is as follows (emphasis mine):
4.10 Pointer conversions [conv.ptr]
1 A null pointer constant is an integral constant expression (5.19) prvalue of integer type that evaluates to zero or a prvalue of type std::nullptr_t. A null pointer constant can be converted to a pointer type; the result is the null pointer value of that type and is distinguishable from every other value of pointer to object or pointer to function type. Such a conversion is called a null pointer conversion.
NULL
is defined to be one such constant:
18.2 Types [support.types]
3 The macro
NULL
is an implementation-defined C++ null pointer constant in this International Standard (4.10). 192192) Possible definitions include
0
and0L
, but not(void*)0
.
explicit auto_ptr(T* p = 0) : ptr(p) {}
This is a constructor for auto_ptr
that takes an T* p
, if the user does not provide a pointer, = 0
will be used. The constructor sets the member ptr
to the argument : ptr(p)
and does nothing else {}
. This constructor cannot be used in implicit constructions explicit
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With