使用const ,引用,函数重载,函数模板,函数默认值,new和delete其中的三个(至少三个)写一个综合型的C++例子!!!
问题描述:
使用const ,引用,函数重载,函数模板,函数默认值,new和delete其中的三个写一个综合型的C++例子!!谢谢各位大佬
答
1、模板函数、const 、&
template
T add(const T & a, const T &b)
{
return a+b;
}
2、自己找一下度娘,一堆实例;还有就是自己多看看概念,正所谓自己动手丰衣足食;
class ClassA
{
public:
ClassA(){ }
ClassA(const char* pszInputStr)
{
pszTestStr = new char[strlen(pszInputStr) + 1];
strncpy(pszTestStr, pszInputStr, strlen(pszInputStr) + 1);
}
~ClassA(){ delete pszTestStr; }
// 赋值运算符重载函数
ClassA& operator=(const ClassA& cls)
{
// 避免自赋值
if (this != &cls)
{
// 避免内存泄露
if (pszTestStr != NULL)
{
delete pszTestStr;
pszTestStr = NULL;
}
pszTestStr = new char[strlen(cls.pszTestStr) + 1];
strncpy(pszTestStr, cls.pszTestStr, strlen(cls.pszTestStr) + 1);
}
return *this;
}
public:
char* pszTestStr;
};
转载自https://blog.csdn.net/liitdar/article/details/80656156