Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the default value of an int* NULL?

Tags:

c++

templates

I have written the following source

#include <iostream>
using namespace std;

template <class T>
class AAA
{
public:
    void f() { cout << T() << " "; }
};

int main ( void )
{
    AAA<int*> a;
    AAA<int> b;

    a.f(); /// in this case, T() == NULL??
    b.f(); 

    return 0;
}

and console print is 00000000 0. ( in visual studio 2010 )

if T is int*, T() == NULL? and is it always true?

like image 681
Donguk Lee Avatar asked Aug 04 '12 17:08

Donguk Lee


2 Answers

This is called value-initialization, you are guaranteed 0.

Plus, you don't need such a complicated example to demonstrate:

typedef int* T;
int main()
{
   T x = T();
   std::cout << x;
}
like image 119
Luchian Grigore Avatar answered Sep 18 '22 12:09

Luchian Grigore


Yes. A value-initialized pointer is always null.

like image 39
Mankarse Avatar answered Sep 22 '22 12:09

Mankarse