假设我有2个头文件,1个.ipp扩展文件和一个main.cpp文件:
第一个头文件(如Java中的接口):
template<class T>
class myClass1{
public:
virtual int size() = 0;
};
Run Code Online (Sandbox Code Playgroud)
第二个头文件:
#include "myClass1.h"
template<class T>
class myClass2 : public myClass1<T>
public:
{
virtual int size();
private:
int numItems;
};
#include "myClass2.ipp"
Run Code Online (Sandbox Code Playgroud)
然后是我的myClass2.ipp文件:
template <class T>
int myClass2<T>::size()
{
return numItems;
}
Run Code Online (Sandbox Code Playgroud)
最后一个是我的主要:
#include "myclass2.h"
void tester()
{
myClass2<int> ForTesting;
if(ForTesting.size() == 0)
{
//......
}
else
{
//.....
}
}
int main(){
tester();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
myClass1,myClass2和myClass2.ipp属于头文件.源文件中的main.cpp.使用这种方式实现程序而不是仅使用.h和.cpp文件有什么好处?什么是.ipp扩展名的文件?.ipp和.cpp之间的区别?
我试图理解C++中的内存部分.我在使用下面的代码生成输出后尝试释放内存.
是否有必要使用if语句释放内存?
int main(){
char *pc;
int *pi;
pc = new char('a');
pi = new int(8);
cout << *pc << endl;
cout << *pi << endl;
//What's the purpose for doing if(pc) and if (pi) below?
if(pc){
delete pc;
}
if(pi){
delete pi;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我可以这样做吗?int main(){char*pc; int*pi;
pc = new char('a');
pi = new int(8);
cout << *pc << endl;
cout << *pi << endl;
delete pc;
delete pi;
return 0;
}
Run Code Online (Sandbox Code Playgroud)