我在 Foo.h
中有一个简单的类定义,例如:
template <typename T>
class Foo
{
public:
Foo();
private:
char *topPtr;
}
我已经像这样实现了 Foo.cpp :
template <typename T>
Foo<T>::Foo(){
cout<<"default constructor is runned"<<endl;
this.topPtr=NULL;
if(topPtr==NULL){cout<<"topPtr is null"<<endl;}
}
现在,为了查看我的 Stack 构造函数是否运行,我编写了一个简单的 main.cpp,如下所示:
#include <iostream>
#include "Foo.h"
using namespace std;
int main(){
Foo<int> foo1();
return 0;
}
我应该在我的终端上看到“默认构造函数已运行” 和“topPtr 为空” 消息,但我什么也没有。有人可以帮助我吗?提前致谢。
最佳答案
声明Foo<int> foo1();
声明一个返回 Foo<int>
的函数 foo1 .你应该这样做:
Foo<int> foo1{};
看这个:Link
还有你的this.topPtr=NULL;
应该是 this->topPtr=NULL;
关于c++ - 检测默认构造函数是否有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20202923/