关于STL中的Traits???

jerrymousenet2 2002-08-14 09:14:54
有没有哪位前辈给我补补课,即关于STL中的Traits的用法。
...全文
80 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
jerrymousenet2 2002-08-18
  • 打赏
  • 举报
回复
up
jerrymousenet2 2002-08-18
  • 打赏
  • 举报
回复
???
jerrymousenet2 2002-08-15
  • 打赏
  • 举报
回复
up,
to be continue...
  • 打赏
  • 举报
回复
准确的写法如下:
要提醒的是,Traits的好处是显而易见的,用得好他会让你的代码 显得十分CLEAN,但滥用就会直接丧失代码的可维护性和可读性..!


template<typename Type>
class TTestClass;

///if int
template<>
class TTestClass<int>
{
public:
typedef int traits_type ;
void ShowTestMessage(const traits_type& t){ ShowMessage(String("Spcial int Type!\n") + t); };
}
;

///else if double
template <>
class TTestClass<double>
{
public:
typedef double traits_type ;
void ShowTestMessage(const traits_type& t){ ShowMessage(String("Spcial double Type!\n") + t);};
};


///*
///else ... Note: it is NOT nessesory!
///if you do not implement the other's type class ,the effect see below button's event
template<typename Type>
class TTestClass
{
public:
typedef Type traits_type ;
void ShowTestMessage(const traits_type& t){ ShowMessage("All Other Types here!"); };
};
//*/


void __fastcall TForm1::Button1Click(TObject *Sender)
{
TTestClass<int> intone;
TTestClass<double> doubleone;
TTestClass<char> charone; //if no implement , compiler will finf an error, stop here...:)

intone.ShowTestMessage(3.2);
doubleone.ShowTestMessage(3.2);
charone.ShowTestMessage(3.2);

}
毕加索的画 2002-08-14
  • 打赏
  • 举报
回复
还是cker高呀
jerrymousenet2 2002-08-14
  • 打赏
  • 举报
回复
看来我是菜鸟中的菜鸟了,
能不能给一些再具体一些的代码。表面上是明白了。但是用起来还是不太明白。谢谢!
coolpony 2002-08-14
  • 打赏
  • 举报
回复
cker兄说的就是透彻,我这样的菜鸟都长进不少。。
:)
  • 打赏
  • 举报
回复
template <char> class Ta
{
....

};

template <int> class Ta
{
....

};


这样就构成了一种语义上的 "模板重载"

使用时
Ta<char> ta;
Ta<int> taa;

Ta<float> ...//出错

这样的情况就是Traits,应该是模板技术,与Stl关系不大

用来对不同类型进行类似 if 类型a else ....逻辑

13,825

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder相关内容讨论区
社区管理员
  • 基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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