已经有一个模板类,我想把里面的某个成员函数再做一个特化模板,该怎么做?

angle_sean1 2011-11-13 11:03:11

using namespace std;
template<class t> class queue;
template<class t> class queue_item{
queue_item(t val=0):item(val),next(0){}
t item;
queue_item<t>* next;
friend class queue<t>;
};
template<class t> class queue{
public:
queue():head(0),tail(0){}
bool empty(){return head == 0;}
t& front(){return head->item;}
void push(const t &val){
queue_item<t> *p = new queue_item<t>(val);
if(empty())
head=tail=p;
else{
tail->next = p;
tail = p;
} }
void pop(){
queue_item<t> *p = head;
head = head->next;
delete p;
}
~queue(){destroy();}
private:
queue_item<t> *head,*tail;
void destroy();
};
template<class t> void queue<t>::destroy(){
while(!empty())
pop();
}
//我想把push和pop函数转门做一个const char*型的特化模板,该怎么做啊,帮我做个好吗,最好类里面定义和类外面定义的两种都让我看看,特化类型是template<>void push<const char*>(const char* p);我知道怎么做不知道放在类里面的格式
...全文
116 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
BT六眼飞鱼 2011-11-13
  • 打赏
  • 举报
回复

void push(const t* val){
queue_item<const t*> *p = new queue_item<const t*>(val);
if(empty())
head=tail=p;
else{
tail->next = p;
tail = p;
}
}

caddor 2011-11-13
  • 打赏
  • 举报
回复
page.27

[Quote=引用 13 楼 caddor 的回复:]
可以的 c++templates page.17页


对于一个 类模板而言,既可以特化整个成员函数,又可以 特化某一个成员函数。。。

但是书上没有给 只特化 成员函数的例子,也没说该怎么写


我自己写了一个,dev通过, 估计没有问题



C/C++ code

#include<iostream>

template<typename T>
class ……
[/Quote]
caddor 2011-11-13
  • 打赏
  • 举报
回复
可以的 c++templates page.17页


对于一个 类模板而言,既可以特化整个成员函数,又可以 特化某一个成员函数。。。

但是书上没有给 只特化 成员函数的例子,也没说该怎么写


我自己写了一个,dev通过, 估计没有问题



#include<iostream>

template<typename T>
class Test
{
public:
void f();
};

template<typename T>
void Test<T>::f()
{
std::cout<<"Test<T>::f()"<<std::endl;
}


template<>
void Test<int>::f()
{
std::cout<<"特化版本的f()"<<std::endl;
}



int main()
{
Test<int> obj;
obj.f();
system("pause");
return 0;
}










[Quote=引用 1 楼 mougaidong 的回复:]
类里面的貌似不可以的
[/Quote]
pengzhixi 2011-11-13
  • 打赏
  • 举报
回复
因为你成员函数本身是一个非模板函数。所以还是特化一个类
pengzhixi 2011-11-13
  • 打赏
  • 举报
回复
直接特化一个queue吧。类似这样

template<>class queue<const char*>{
public:
void push(const char*)
{
cout<<"char*"<<endl;
}
};
woshiwaiwai 2011-11-13
  • 打赏
  • 举报
回复
template<> void queue<char *>::pop()
{
}
template<> void queue<const char *>::push(const char* const &val)
{
}
~~
woshiwaiwai 2011-11-13
  • 打赏
  • 举报
回复
在类里面不需要再写什么
在类外面定义这两个函数 然后在里面 写你想做的事
void queue<const char *>::pop()
{
}
void queue<const char *>::push(const char* const &val)
{
}
这就是push和pop的const char *特化版本
angle_sean1 2011-11-13
  • 打赏
  • 举报
回复
它C++Primer第四版 569页就是弄了一个push和pop的特化版本,书上还特意说明有2种方法,一种是声明一个queue的特化类,另一种是只构造push和pop的特化版本,所以我才纳闷了,你构造一个push和pop的特化版本该怎么放入类呢,根本不可能的事嘛,应该是书上SB了
xingfeng2510 2011-11-13
  • 打赏
  • 举报
回复
为模板类queue添加特化类:
template<> class queue<const char*>{
...
};

如果想要将偏特化版本的函数直接添加到模板类queue中不可行,只好添加偏特化类或者在类定义之外添加特定模板函数的偏特化函数。
angle_sean1 2011-11-13
  • 打赏
  • 举报
回复
大哥们,到底是怎么一个情况啊。。。。。能不能啊
woshiwaiwai 2011-11-13
  • 打赏
  • 举报
回复
弄错了 好想都可以
void queue<const char *>::pop() pop可以
void queue<const char *>::push(const char* const &val) push
woshiwaiwai 2011-11-13
  • 打赏
  • 举报
回复
void queue<const char *>::pop() pop可以
push好像C++中不支持类模板中的函数模板特化。
ryfdizuo 2011-11-13
  • 打赏
  • 举报
回复
首先成员函数必须是函数模板才能特化啊。
看你的程序 根本没必要搞这么复杂。。。
turing-complete 2011-11-13
  • 打赏
  • 举报
回复
类里面的貌似不可以的

64,683

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧