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 : { : // ...
|