Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What do default values of pointers in constructors mean?

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).

like image 393
Roman Avatar asked Apr 05 '13 14:04

Roman


People also ask

What is a pointers default value?

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.)

What is the default value of a constructor?

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() .

What is default constructor significance?

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.

What are pointers default initialized to?

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.


2 Answers

Yes, = 0 is the default value. For a pointer argument, it is the same as = NULL.

To quote Stroustrup:

Should I use NULL or 0?

In C++, the definition of NULL is 0, so there is only an aesthetic difference. I prefer to avoid macros, so I use 0. Another problem with NULL is that people sometimes mistakenly believe that it is different from 0 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). 192

192) Possible definitions include 0 and 0L, but not (void*)0.

like image 123
NPE Avatar answered Sep 23 '22 18:09

NPE


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.

like image 42
David Rodríguez - dribeas Avatar answered Sep 21 '22 18:09

David Rodríguez - dribeas