我写了以下来源
#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?它总是如此吗?
这称为值初始化,您可以保证0.
另外,您不需要这么复杂的示例来演示:
typedef int* T;
int main()
{
T x = T();
std::cout << x;
}
Run Code Online (Sandbox Code Playgroud)