模板参数的多态性 [英] polymorphism on template parameters

查看:85
本文介绍了模板参数的多态性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



大家好,


我正在摆弄政策,但我遇到了一些问题...


我的代码:


class MovePolicy {};

class EatPolicy {};

class ReproducePolicy { };


模板<

类Move = MovePolicy,

class Eat = EatPolicy,

class Reproduce = ReproducePolicy


>



class Thing:public Move,Eat,Reproduce

{

public:

Thing(){}

virtual~Thing(){}

};


class NoMove:public MovePolicy { };

类NoEat:public EatPolicy {};

class NoReproduce:public ReproducePolicy {};


class Stone:public事情< NoMove,NoEat,NoReproduce>

{

public:

Stone(){}

~ Stone(){}

};


int

main(){

Thing< > * s = new Stone();

返回0;

}


我收到错误:


在函数中?? ?? main()a ??:

错误:无法转换?? Stone * a ?? to a ?? Thing< MovePolicy,EatPolicy,

ReproducePolicy> * a ??在初始化中

对主题的任何指针(不以0x开头......)表示赞赏。


欢呼,

- renato


-

回收您的数字版权,消除DRM,了解更多信息
http://www.defectivebydesign.org/what_is_drm

解决方案



附录:这当然有效......但是我不需要给予我自由的好处

来做我想做的事。

class MovePolicy {};

class EatPolicy {};

class ReproducePolicy {};


class NoMove:public MovePolicy {};

class NoEat:public EatPolicy {};

class NoReproduce:public ReproducePolicy {};


模板<

class Move = NoMove,

class Eat = NoEat,

class Reproduce = NoReproduce


>



class Thing:public Move,Eat,Reproduce

{

public:

东西(){}

虚拟〜东西(){}

};


类石头:public Thing< ; NoMove,NoEat,NoReproduce>

{

public:

Stone(){}

~Stone( ){}

};


int

main(){

Thing<> * s = new Stone();

返回0;

}

欢呼,

--renato

-

回收您的数字版权,消除DRM,了解更多信息
http://www.defectivebydesign.org/what_is_drm


Renato Golin< re ***** *@gmail.com写在

新闻:48 ********************* @ newsread.sanger.ac.uk:


>

大家好,


我正在摆弄政策,但我我遇到了一些问题...


我收到错误:


在函数a中?int main()a ?? :

错误:无法转换??石头* a ?? to a ?? Thing< MovePolicy,EatPolicy,

ReproducePolicy> * a ??在初始化中


关于这个主题的任何指针(不是以0x开头......)都赞赏。

欢呼,

- renato



可悲的是,这既是基于政策的设计的诅咒和祝福。 />
不同的模板参数创建全新的类型。模板

名称本身并不意味着实例之间的交互能力。

这是TR1中的shared_ptr<基于策略的原因之一

智能指针。


关于如何整理这些

模板功能,我有两条建议。


首先,您可以使用构造函数配置/设置的策略来代替策略。换句话说,如果你想让结果类型互动,好像它们是相同类型的

,就不要以这种方式使用模板




或者,你可以为基本的''Thing''定义一个接口 - 并且使你的模板化类继承的所有

只使用

建设。 ie


class IThing

{

public:

virtual void DoThing()= 0;

虚拟〜IThing(){}

};


class MovePolicy {};

class EatPolicy {};

class ReproducePolicy {};


template<

class Move = MovePolicy,

class Eat = EatPolicy,

class Reproduce = ReproducePolicy


>



class Thing:public IThing,Move,Eat,Reproduce

{

public:

Thing(){}

virtual~Thing(){}

虚拟DoThing(){}

};


类NoMove:public MovePolicy {};

class NoEat:public EatPolicy {};

class NoReproduce:public ReproducePolicy {};


class Stone:public Thing< NoMove,NoEat,NoReproduce>

{

public:

Stone(){}

~Stone(){}

};


int

main(){

IThing * s = static_cast< IThing *>(new Stone());

s - > DoThing();

删除s; //虚拟析构函数让这个工作

返回0;

}


HTH,

joe


On 2008-08-05 08:46:12 -0400,Renato Golin< re ****** @ gmail.comsaid:
< blockquote class =post_quotes>
>

class Stone:public Thing< NoMove,NoEat,NoReproduce>

{
$ b $公开:

Stone(){}

~Stone(){}

};


int

main(){

Thing<> * s = new Stone();

返回0;

}



首先,当事情开始变得混乱时,减少模板

参数的数量。这样可以更容易地看到发生了什么。在您确定了b / b $ b $之后,重新添加其余细节。


>

我收到错误:


函数a ?? int main()a ??:

错误:无法转换?? Stone * a ?? to a ?? Thing< MovePolicy,EatPolicy,

ReproducePolicy> * a ??在初始化中



Thing< MovePolicy等和Thing< NoMove等是两种不相关的类型。

此代码中出现同样的问题:


class Base1 {};

class Base2 {};

class Derived:public Base1 {};

Base2 * ptr = new派生; //错误:没有从Derived *转换为Base2 *


-

Pete

Roundhouse Consulting,Ltd。(< a rel =nofollowhref =http://www.versatilecoding.comtarget =_ blank> www.versatilecoding.com )作者

标准C ++图书馆扩展:教程和参考

www.petebecker.com/ tr1book



Hi all,

I''m fiddling with policies but I''m having some problems...

My Code:

class MovePolicy { };
class EatPolicy { };
class ReproducePolicy { };

template <
class Move = MovePolicy,
class Eat = EatPolicy,
class Reproduce = ReproducePolicy

>

class Thing : public Move, Eat, Reproduce
{
public:
Thing() { }
virtual ~Thing() { }
};

class NoMove : public MovePolicy { };
class NoEat : public EatPolicy { };
class NoReproduce : public ReproducePolicy { };

class Stone : public Thing <NoMove, NoEat, NoReproduce>
{
public:
Stone() { }
~Stone() { }
};

int
main () {
Thing<>* s = new Stone();
return 0;
}

I get the error:

In function a??int main()a??:
error: cannot convert a??Stone*a?? to a??Thing<MovePolicy, EatPolicy,
ReproducePolicy>*a?? in initialization
Any pointers (that doesn''t start with 0x...) on the subject are appreciated.

cheers,
--renato

--
Reclaim your digital rights, eliminate DRM, learn more at
http://www.defectivebydesign.org/what_is_drm

解决方案


Addendum: This works, of course... but won''t give me the freedom I need
to do what I want.
class MovePolicy { };
class EatPolicy { };
class ReproducePolicy { };

class NoMove : public MovePolicy { };
class NoEat : public EatPolicy { };
class NoReproduce : public ReproducePolicy { };

template <
class Move = NoMove,
class Eat = NoEat,
class Reproduce = NoReproduce

>

class Thing : public Move, Eat, Reproduce
{
public:
Thing() { }
virtual ~Thing() { }
};

class Stone : public Thing <NoMove, NoEat, NoReproduce>
{
public:
Stone() { }
~Stone() { }
};

int
main () {
Thing<>* s = new Stone();
return 0;
}
cheers,
--renato
--
Reclaim your digital rights, eliminate DRM, learn more at
http://www.defectivebydesign.org/what_is_drm


Renato Golin <re******@gmail.comwrote in
news:48*********************@newsread.sanger.ac.uk :

>
Hi all,

I''m fiddling with policies but I''m having some problems...

I get the error:

In function a??int main()a??:
error: cannot convert a??Stone*a?? to a??Thing<MovePolicy, EatPolicy,
ReproducePolicy>*a?? in initialization
Any pointers (that doesn''t start with 0x...) on the subject are
appreciated.

cheers,
--renato

Sadly, that is both the curse and blessing of policy based design.
Different template parameters create completely new types. The template
name itself doesn''t imply interaction capability between the instances.
That is one of the reasons that shared_ptr<in TR1 isn''t a policy based
smart pointer.

I have two suggestions as to how to organize things to get around this
feature of templates.

First, Instead of policies, you can use strategies that get
configured/set by the constructor. In other words, don''t use templates
in this way if you want the resulting types to interact as if they were
the same type.

Or, you can define an interface for the basic ''Thing''-ness and make all
your templated classes inherit from that and only use the template for
construction. i.e.

class IThing
{
public:
virtual void DoThing() = 0;
virtual ~IThing() {}
};

class MovePolicy { };
class EatPolicy { };
class ReproducePolicy { };

template <
class Move = MovePolicy,
class Eat = EatPolicy,
class Reproduce = ReproducePolicy

>

class Thing : public IThing, Move, Eat, Reproduce
{
public:
Thing() { }
virtual ~Thing() { }
virtual DoThing() {}
};

class NoMove : public MovePolicy { };
class NoEat : public EatPolicy { };
class NoReproduce : public ReproducePolicy { };

class Stone : public Thing <NoMove, NoEat, NoReproduce>
{
public:
Stone() { }
~Stone() { }
};

int
main () {
IThing * s = static_cast<IThing *>(new Stone());
s ->DoThing();
delete s; // virtual destructor lets this work
return 0;
}

HTH,
joe


On 2008-08-05 08:46:12 -0400, Renato Golin <re******@gmail.comsaid:

>
class Stone : public Thing <NoMove, NoEat, NoReproduce>
{
public:
Stone() { }
~Stone() { }
};

int
main () {
Thing<>* s = new Stone();
return 0;
}

First, when things start getting tangled, reduce the number of template
arguments. That makes it easier to see what''s going on. After you''ve
figured things out, add the rest of the details back in.

>
I get the error:

In function a??int main()a??:
error: cannot convert a??Stone*a?? to a??Thing<MovePolicy, EatPolicy,
ReproducePolicy>*a?? in initialization

Thing<MovePolicy etc.and Thing<NoMove etc.are two unrelated types.
The same problem occurs in this code:

class Base1 { };
class Base2 { };
class Derived : public Base1 { };
Base2 *ptr = new Derived; // error: no conversion from Derived* to Base2*

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)


这篇关于模板参数的多态性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆