int*NULL的默认值是什么?

Don*_*Lee 6 c++ templates

我写了以下来源

#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;
}
Run Code Online (Sandbox Code Playgroud)

和控制台打印是00000000 0.(在visual studio 2010中)

如果T是int*,T()== NULL?它总是如此吗?

Luc*_*ore 9

这称为值初始化,您可以保证0.

另外,您不需要这么复杂的示例来演示:

typedef int* T;
int main()
{
   T x = T();
   std::cout << x;
}
Run Code Online (Sandbox Code Playgroud)

  • 上帝,55k的声誉,你仍然在不到一分钟的时间里回答那些愚蠢的问题.你为什么不把你的知识留给更复杂的问题,让新人有机会建立一些声誉? (2认同)

Man*_*rse 8

是.值初始化指针始终为null.