我写了如下源
#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;
}
控制台打印为 00000000 0。(在 visual studio 2010 中)
如果 T 是 int*,T() == NULL?它总是正确的吗?
最佳答案
这称为值初始化,您保证0
。
另外,你不需要这么复杂的例子来演示:
typedef int* T;
int main()
{
T x = T();
std::cout << x;
}
关于c++ - int* 的默认值是 NULL 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11810609/