6

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?

Ry-
  • 218,210
  • 55
  • 464
  • 476
Donguk Lee
  • 61
  • 1
  • 2
  • 1
    Yup. Always. http://stackoverflow.com/a/937257/707111 – Ry- Aug 04 '12 at 17:33
  • possible duplicate of [What is the default constructor for C++ pointer?](http://stackoverflow.com/questions/936999/what-is-the-default-constructor-for-c-pointer) – Troubadour Aug 04 '12 at 17:48

3 Answers3

9

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;
}
Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • 2
    God, 55k reputation and you still answer those silly question in less than a minute. Why don't you keep your knowledge for more complicated question, giving to newcomers the chance to build some reputation? – akappa Aug 04 '12 at 17:49
  • I see. I upvoted your answer, to support reaching your milestone :P – akappa Aug 04 '12 at 18:23
8

Yes. A value-initialized pointer is always null.

Mankarse
  • 39,818
  • 11
  • 97
  • 141
0

Yes. It is null pointer. And why it is printing:

00000000

because it is using 4-byte to represent the null-address (in hexadecimal format).

On 64-bit machine, it may print this instead (hexadecimal format):

0000000000000000
Nawaz
  • 353,942
  • 115
  • 666
  • 851