- 论坛徽章:
- 1
|
windoze 发表于 2014-11-20 21:51 ![]()
回复 102# lost_templar
开销还在其次,关键是太丑。
为什么你觉得太丑?我觉得这样写很舒服
设想你一个类有100个方法,声明在一个头文件里边
- struct big_class
- {
- virtual void method_1(){...}
- virtual void method_2(){...}
- //......
- virtual void method_100{...}
- };
复制代码 这个文件会非常庞大。
然后再对这个 big_class 做点继承或者维护什么的,更是非常麻烦。
而如果将这100个接口分散到 100 个不同的文件中去
- //file method_1.hp
- template< tyename T >
- struct method_1_impl
- {
- void method_1(){....}
- };
- //....
- //file method_100.hpp
- template< tyename T >
- struct method_100_impl
- {
- void method_100(){....}
- };
复制代码 那么使用的时候只要选择必要的几个 method 组合一下就成了
- struct my_text_editor : method_23_impl<my_text_editor>, method_72_impl<my_text_editor>
- {};
- my_text_editor mte;
- mte.method_23();
- mte.method_72();
复制代码 在我看来,这比继承一个有 100 虚函数的类要优雅得多。
|
|