template<typename T1>
class MyObjectT
{
public:
template<typename T2>
void Foo(T2 v);
};
template<typename T1>
template<typename T2>
void MyObjectT<T1>::Foo(T2 v)
{
cout << "Foo(" << v << ")" << endl;
}
#include <iostream>
class Single{
public:
static Single* ShareInstance();
static void ReleaseInstance();
template<class Tex>//要成员函数实现模版用法,而类不是模版类。需要写成这个格式
void SwapEx(Tex &obj1, Tex &obj2){
}
private:
Single();
~Single();
Single(const Single &other);
Single& operator=(const Single &other);
private:
static Single *m_pObj;
};
需要放在同一个文件
这篇博客探讨了C++中模板类的使用,通过`MyObjectT`类展示了如何定义和实现一个模板成员函数`Foo`。同时,文章还介绍了单例模式的实现,`Single`类作为非模板类提供了静态成员函数`ShareInstance`和`ReleaseInstance`,以及一个模板成员函数`SwapEx`用于对象交换。示例代码详细地展示了这些概念的实际应用。
672

被折叠的 条评论
为什么被折叠?



