由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 这个是什么设计模式?
相关主题
问一个C++的问题a c++ question
find bugs of c++ codesAny difference between class and typename identifier?
Help: who has gcc 4.0 or higher[菜鸟问题]类模板问题
template 类的继承问题文一个简单的c++
模板对象能不能作为成员变量使用where to define my template function
一个C++ template的问题C++ template question
c++ 如何重用一段代码,并把其中加法变成减法thrust help ~~~
请教一个关于std::function的问题c++ template question:
相关话题的讨论汇总
话题: template话题: class话题: typea话题: 设计模式
进入Programming版参与讨论
1 (共1页)
A*******e
发帖数: 2419
1
在好几个同事的C++代码里看到类似的设计。
class FooInterface {
...
};
template
class FooTemplate: public FooInterface {
...
};
class FooImpForTypeA: pulbic FooTemplate {
...
};
l*********s
发帖数: 5409
2
curious recurring template pattern
x****k
发帖数: 2932
3
不一样吧,crpt的应该是
// The Curiously Recurring Template Pattern (CRTP)
template
class Base
{
// methods within Base can use template to access members of Derived
};
class Derived : public Base
{
// ...
};
template paramter of Base class template is class Derived.
https://en.wikipedia.org/wiki/Curiously_recurring_template_pattern

【在 l*********s 的大作中提到】
: curious recurring template pattern
d****i
发帖数: 4809
4
貌似是template specialization。(模板定制对于某个特殊的类型TypeA)

【在 A*******e 的大作中提到】
: 在好几个同事的C++代码里看到类似的设计。
: class FooInterface {
: ...
: };
: template
: class FooTemplate: public FooInterface {
: ...
: };
: class FooImpForTypeA: pulbic FooTemplate {
: ...

p******g
发帖数: 347
5
(one of the?) the purpose is to define the interface Foo for all the classes
that want to have the interface but NOT to derive from Foo. So that
FooImpForTypeA* and FooImpForTypeB* can NOT be represented by the
FooInterface*
it is useful when TypeA and TypeB both have same interface but they behave
totally differently. and one don't want to accidentally pass a TypeB to a
piece of codes where TypeA is expected (by accepting FooInterface* ).

【在 A*******e 的大作中提到】
: 在好几个同事的C++代码里看到类似的设计。
: class FooInterface {
: ...
: };
: template
: class FooTemplate: public FooInterface {
: ...
: };
: class FooImpForTypeA: pulbic FooTemplate {
: ...

l*********s
发帖数: 5409
6
对,看错了

【在 x****k 的大作中提到】
: 不一样吧,crpt的应该是
: // The Curiously Recurring Template Pattern (CRTP)
: template
: class Base
: {
: // methods within Base can use template to access members of Derived
: };
: class Derived : public Base
: {
: // ...

1 (共1页)
进入Programming版参与讨论
相关主题
c++ template question:模板对象能不能作为成员变量使用
c++ template question:一个C++ template的问题
私有成员不能用类成员函数修改?c++ 如何重用一段代码,并把其中加法变成减法
谁给详细说一下这句请教一个关于std::function的问题
问一个C++的问题a c++ question
find bugs of c++ codesAny difference between class and typename identifier?
Help: who has gcc 4.0 or higher[菜鸟问题]类模板问题
template 类的继承问题文一个简单的c++
相关话题的讨论汇总
话题: template话题: class话题: typea话题: 设计模式