Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   template and dynamic polymorphism (http://www.velocityreviews.com/forums/t630258-template-and-dynamic-polymorphism.html)

Jun 08-11-2008 07:38 PM

template and dynamic polymorphism
 
struct Abstract{

virtual void run() const = 0;

};

template <typename T>
struct S : F{
void run(){}
};


I just want to use as :

Abstract* a = new S<policy_1>(); // doesn't work


I just wanna to know how i could mix the templates with the
traditional abstract class design, thank you in advance.

Jun 08-11-2008 08:09 PM

Re: template and dynamic polymorphism
 
Sorry, the code is :


> struct Abstract{
>
> * * virtual void run() const = 0;
>
> };
>
> template <typename T>
> struct S : Abstract{
> * * void *run(){}
>
> };
>
> I just want to use as :
>
> Abstract* a = new S<policy_1>(); // doesn't work
>
> I just wanna to know how i could mix the templates with the
> traditional abstract class design, thank you in advance.



Jun 08-11-2008 08:10 PM

Re: template and dynamic polymorphism
 
On Aug 11, 9:46*pm, "Alf P. Steinbach" <al...@start.no> wrote:
> * Jun:
>
>
>
> > struct Abstract{

>
> > * * virtual void run() const = 0;

>
> > };

>
> > template <typename T>
> > struct S : F{
> > * * void *run(){}
> > };

>
> > I just want to use as :

>
> > Abstract* a = new S<policy_1>(); // doesn't work

>
> > I just wanna to know how i could mix the templates with the
> > traditional abstract class design, thank you in advance.

>
> How about deriving S from Abstract.
>
> Cheers, & hth.,
>
> - Alf
>
> --
> A: Because it messes up the order in which people normally read text.
> Q: Why is it such a bad thing?
> A: Top-posting.
> Q: What is the most annoying thing on usenet and in e-mail?


Sorry, my code is actually :

struct S : Abstract // and still doesn't work

Leandro Melo 08-11-2008 08:13 PM

Re: template and dynamic polymorphism
 
On 11 ago, 17:09, Jun <junh...@gmail.com> wrote:
> Sorry, the code is :
>
> > struct Abstract{

>
> > * * virtual void run() const = 0;

>
> > };

>
> > template <typename T>
> > struct S : Abstract{
> > * * void *run(){}

>
> > };

>
> > I just want to use as :

>
> > Abstract* a = new S<policy_1>(); // doesn't work

>
> > I just wanna to know how i could mix the templates with the
> > traditional abstract class design, thank you in advance.

>
>


You get the error because you're hidding run() from class abstract (in
S you're not making the function const). Try this:
template <typename T>
struct S : Abstract
{
void run()const {} //Notice the const at the end.
};


Rolf Magnus 08-11-2008 08:14 PM

Re: template and dynamic polymorphism
 
Alf P. Steinbach wrote:

> * Jun:
>> struct Abstract{
>>
>> virtual void run() const = 0;
>>
>> };
>>
>> template <typename T>
>> struct S : F{
>> void run(){}
>> };
>>
>>
>> I just want to use as :
>>
>> Abstract* a = new S<policy_1>(); // doesn't work
>>
>>
>> I just wanna to know how i could mix the templates with the
>> traditional abstract class design, thank you in advance.

>
> How about deriving S from Abstract.


Another thing that comes to mind would be to make run() in S const, like in
Abstract, as well as telling, what "doesn't work" exactly means in this
case.



Jun 08-11-2008 08:39 PM

Re: template and dynamic polymorphism
 
On Aug 11, 10:14*pm, Rolf Magnus <ramag...@t-online.de> wrote:
> Alf P. Steinbach wrote:
> > * Jun:
> >> struct Abstract{

>
> >> * * virtual void run() const = 0;

>
> >> };

>
> >> template <typename T>
> >> struct S : F{
> >> * * void *run(){}
> >> };

>
> >> I just want to use as :

>
> >> Abstract* a = new S<policy_1>(); // doesn't work

>
> >> I just wanna to know how i could mix the templates with the
> >> traditional abstract class design, thank you in advance.

>
> > How about deriving S from Abstract.

>
> Another thing that comes to mind would be to make run() in S const, like in
> Abstract, as well as telling, what "doesn't work" exactly means in this
> case.


Thanks a lot, after changing the run as const in S, it works fine.


Noah Roberts 08-12-2008 05:04 PM

Re: template and dynamic polymorphism
 
Jun wrote:
> On Aug 11, 10:14 pm, Rolf Magnus <ramag...@t-online.de> wrote:
>> Alf P. Steinbach wrote:
>>> * Jun:
>>>> struct Abstract{
>>>> virtual void run() const = 0;
>>>> };
>>>> template <typename T>
>>>> struct S : F{
>>>> void run(){}
>>>> };
>>>> I just want to use as :
>>>> Abstract* a = new S<policy_1>(); // doesn't work
>>>> I just wanna to know how i could mix the templates with the
>>>> traditional abstract class design, thank you in advance.
>>> How about deriving S from Abstract.

>> Another thing that comes to mind would be to make run() in S const, like in
>> Abstract, as well as telling, what "doesn't work" exactly means in this
>> case.

>
> Thanks a lot, after changing the run as const in S, it works fine.
>


This will repeatedly mess with you. Make it the first thing you check.


All times are GMT. The time now is 04:50 AM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.