C_and_CPP 板


LINE

唔,还是讲一下好了 ※ 引述《yoco315 (眠月)》之铭言: : 很简单的继承,Foo 继承 Bar,没有指明继承的形式表示是 public 继承。 ^^^^^^ 是 private 推文说了,建议修文 : ┌─────────────────────────── : │template < class T > : │class Foo : T : │{ : │ ... : │} ; : └─────────────────────────── : 看起来很玄,其实就是 Foo 继承 T, : 不过这个 T 是个 template, 我会说 T 是个 class 而非 template, 就像上篇我在推文说的 理由出自 template template (or more template) parameter template < template <class> class T > class Foo; T 是什麽?不是 class, 是 template Foo<int> foo; // error, int is not template template <class T> class Bar; Foo<Bar> foo; // ok, Bar is template Bar<Bar> bar; // error, Bar is not class Bar< Bar<int> > // ok, Bar<int> is class 可以玩更复杂一点 template < template < template<class> class T > class U > class Test{}; template < template<class> class T > class Test2: private T<int>{}; // 写 T 不行,因为不能继承 template Test<Test2> test; // ok, Test2 is template template : Loki::Typelist 还有用到类似这样的写法 : ┌─────────────────────────── : │template < class T, class U > : │Foo : T < U > : │{ : │} : └─────────────────────────── 不对吧,Typelist 是这样做的 template <class Head, class Tail> class Typelist{}; 然後是这样串的 Typelist< int, Typelist<int, Typelist<int, Typelist<int, NullType> > > > 头咬尾巴这样 你说的东西後述 : 这可以用来实作 Typelist 技术。 ^^^^^^^^ 是 Tuple : ┌─────────────────────────── : │template < typenamep T > : │class Holder : │{ : │ private: T value_ ; : │ public: T & value() { return value_ ; } : │} ; : │ : │typedef TYPELIST_3 ( Array2D, Array2D, Array2D ) RGB_Typelist ; : │typedef TYPELIST_3 ( Array2D, Array2D, Array2D ) HSV_Typelist ; : │typedef TYPELIST_3 ( Array2D, Array2D, Array2D ) Lab_Typelist ; : │typedef TYPELIST_4 ( Array2D, Array2D, Array2D, Array2D ) YMCK_Typelist ; : │ : │typedef Loki::GenScatterHierarchy < RGB_Typelist , Holder > RGB_Image ; : │typedef Loki::GenScatterHierarchy < HSV_Typelist , Holder > HSV_Image ; : │typedef Loki::GenScatterHierarchy < Lab_Typelist , Holder > Lab_Image ; : │typedef Loki::GenScatterHierarchy < YMCK_Typelist, Holder > YMCK_Image ; : └─────────────────────────── 参考 Loki::Tuple, 你说的这个跟 Loki::Tuple 做法很接近(其实算一样了) 而这里的 Typelist 是用头咬尾做的,GenScatterHierarchy 才是运用继承技术 不过真正的实作法很复杂,这里就不说明了…(其实是我还没完全看懂 Orz) : RGB_Image img ; : Loki::Field<0>(img).value() = ... : Loki::Field<1>(img).value() = ... : Loki::Field<2>(img).value() = ... Loki 的 Tuple 的 holder 是这样写的 template <class T> struct TupleUnit { T value_; operator T&() { return value_; } operator const T&() const { return value_; } }; 所以用 Loki::Tuple 的话是这样写 RGB_Image img; Field<0>(img) = 255; Field<1>(img) = 255; Field<2>(img) = 255; Loki:: 可以省略,因为 Koenig Lookup... : 真是酷! : Loki::Typelist 会根据你给的 Typelist, : (就是一串型别,像是这样 int, int, double, Foo, Bar ) : 使用递回继承自己的方法, 这是 GenScatterHierarchy... GenScatterHierarchy 会依照 Typelist 里面的型别依序继承 Typelist 就只是型别串列而已 : 实作细节有兴趣的话可以看 《Modern C++ Design》。 indeed... 不过看原始码更快 http://sourceforge.net/projects/loki-lib 现在 0.14 了,里面多一堆书上没写的东西 Sutter 的 Pimpl 也有(0.14 新增) ScopeGuard(0.13 新增) 还有一堆有兴趣自己看吧… -- Nobody can take anything away from him. Nor can anyone give anything to him. What came from the sea, has returned to the sea. Chrono Cross --



※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 140.138.19.204 ※ 编辑: godfat 来自: 140.138.19.204 (04/18 14:32)







like.gif 您可能会有兴趣的文章
icon.png[问题/行为] 猫晚上进房间会不会有憋尿问题
icon.pngRe: [闲聊] 选了错误的女孩成为魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一张
icon.png[心得] EMS高领长版毛衣.墨小楼MC1002
icon.png[分享] 丹龙隔热纸GE55+33+22
icon.png[问题] 清洗洗衣机
icon.png[寻物] 窗台下的空间
icon.png[闲聊] 双极の女神1 木魔爵
icon.png[售车] 新竹 1997 march 1297cc 白色 四门
icon.png[讨论] 能从照片感受到摄影者心情吗
icon.png[狂贺] 贺贺贺贺 贺!岛村卯月!总选举NO.1
icon.png[难过] 羡慕白皮肤的女生
icon.png阅读文章
icon.png[黑特]
icon.png[问题] SBK S1安装於安全帽位置
icon.png[分享] 旧woo100绝版开箱!!
icon.pngRe: [无言] 关於小包卫生纸
icon.png[开箱] E5-2683V3 RX480Strix 快睿C1 简单测试
icon.png[心得] 苍の海贼龙 地狱 执行者16PT
icon.png[售车] 1999年Virage iO 1.8EXi
icon.png[心得] 挑战33 LV10 狮子座pt solo
icon.png[闲聊] 手把手教你不被桶之新手主购教学
icon.png[分享] Civic Type R 量产版官方照无预警流出
icon.png[售车] Golf 4 2.0 银色 自排
icon.png[出售] Graco提篮汽座(有底座)2000元诚可议
icon.png[问题] 请问补牙材质掉了还能再补吗?(台中半年内
icon.png[问题] 44th 单曲 生写竟然都给重复的啊啊!
icon.png[心得] 华南红卡/icash 核卡
icon.png[问题] 拔牙矫正这样正常吗
icon.png[赠送] 老莫高业 初业 102年版
icon.png[情报] 三大行动支付 本季掀战火
icon.png[宝宝] 博客来Amos水蜡笔5/1特价五折
icon.pngRe: [心得] 新鲜人一些面试分享
icon.png[心得] 苍の海贼龙 地狱 麒麟25PT
icon.pngRe: [闲聊] (君の名は。雷慎入) 君名二创漫画翻译
icon.pngRe: [闲聊] OGN中场影片:失踪人口局 (英文字幕)
icon.png[问题] 台湾大哥大4G讯号差
icon.png[出售] [全国]全新千寻侘草LED灯, 水草

请输入看板名称,例如:BabyMother站内搜寻

TOP