Mix Static and dynamic Polymorphism
The group you are posting to is a
Usenet group . Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
Newsgroups: comp.lang.c++.moderated
From:
Jun <junh... @gmail.com>
Date: Sat, 7 Nov 2009 09:16:28 CST
Local: Sat 7 Nov 2009 15:16
Subject: Mix Static and dynamic Polymorphism
Hello all, I just tried to mix static and dynamic polymorphism together.
I've a vector to store all the elements, and the implementations of elements are slightly different. I used Policy-based design:
struct Interface{ virtual void run(void) = 0; virtual void execute(void) = 0;
};
struct PolicyA{ void execute(){ // implementing Policy A; }
};
struct PolicyB{ void execute(){ // implementing Policy B; }
};
template <class Policy> struct Base : public Policy, public Interface{ void run(){ // implementing base ; }
}
Anyway, those codes don't compile .... Anyone has some suggestions ? Thank you in advance. Jun
-- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
You must
Sign in before you can post messages.
You do not have the permission required to post.
Newsgroups: comp.lang.c++.moderated
From:
Neil Butterworth <nbutterworth1... @gmail.com>
Date: Sat, 7 Nov 2009 15:22:44 CST
Local: Sat 7 Nov 2009 21:22
Subject: Re: Mix Static and dynamic Polymorphism
Jun wrote:
> struct Interface{
> virtual void run(void) = 0;
> virtual void execute(void) = 0;
> };
> struct PolicyA{ > void execute(){ > // implementing Policy A; > } > };
> struct PolicyB{ > void execute(){ > // implementing Policy B; > } > };
> template <class Policy> > struct Base : public Policy, public Interface{ > void run(){ > // implementing base ; > } > }
> Anyway, those codes don't compile .... Anyone has some suggestions ?
With the addition of a semicolon to the end of the Base struct: template <class Policy> struct Base : public Policy, public Interface{ void run(){ // implementing base ; }
};
your code compiles for me with g++ 4.4.0. What compilation errors are you getting? Neil Butterworth
-- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
You must
Sign in before you can post messages.
You do not have the permission required to post.
Newsgroups: comp.lang.c++.moderated
From:
Jun <junh... @gmail.com>
Date: Sat, 7 Nov 2009 18:23:17 CST
Local: Sun 8 Nov 2009 00:23
Subject: Re: Mix Static and dynamic Polymorphism
{ Please avoid top-posting, and don't quote the clc++m banner. -mod } Hello all,
I just reach a solution that simply regard interface::execute as a wrapped member function, name it different from PolicyA::execute and wrap it inside:
struct Interface{ virtual void run() = 0; virtual void executeInterface() = 0;
}
template <class Policy> struct Base : public Policy, public Interface{ using Policy::execute; void run(){ // implementing base ; } void executeInterface(){ execute(); }
}
This solution is just a little bit "ugly", could someone find a solution that compose the Interface class ? Thank you in advance. Jun
On Nov 7, 11:16 am, Jun <junh... @gmail.com> wrote:
> Hello all,
> I just tried to mix static and dynamic polymorphism together.
> I've a vector to store all the elements, and the implementations > of elements are slightly different. I used Policy-based design:
> struct Interface{ > virtual void run(void) = 0; > virtual void execute(void) = 0;
> };
> struct PolicyA{ > void execute(){ > // implementing Policy A; > }
> };
> struct PolicyB{ > void execute(){ > // implementing Policy B; > }
> };
> template <class Policy> > struct Base : public Policy, public Interface{ > void run(){ > // implementing base ; > }
> }
> Anyway, those codes don't compile .... Anyone has some suggestions ? > Thank you in advance.
> Jun
> -- > [ Seehttp://www.gotw.ca/resources/clcm.htmfor info about ] > [ comp.lang.c++.moderated. First time posters: Do this! ]
-- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
You must
Sign in before you can post messages.
You do not have the permission required to post.
Newsgroups: comp.lang.c++.moderated
From:
tohava <toh... @gmail.com>
Date: Sat, 7 Nov 2009 18:24:14 CST
Local: Sun 8 Nov 2009 00:24
Subject: Re: Mix Static and dynamic Polymorphism
On Nov 7, 5:16 pm, Jun <junh... @gmail.com> wrote:
> Anyway, those codes don't compile .... Anyone has some suggestions ? > Thank you in advance.
First, it would be wise to include the compilation errors. Second, I'd recommend rewriting as: struct Interface{ virtual void run(void) = 0; virtual void execute(void) = 0;
};
struct PolicyA : public Interface { void execute(){ // implementing Policy A; }
};
struct PolicyB : public Interface { void execute(){ // implementing Policy B; }
};
template <class Policy> struct Base : public Policy { void run(){ // implementing base ; }
}
-- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
You must
Sign in before you can post messages.
You do not have the permission required to post.
Newsgroups: comp.lang.c++.moderated
From:
Maxim Yegorushkin <maxim.yegorush... @gmail.com>
Date: Sat, 7 Nov 2009 18:24:34 CST
Local: Sun 8 Nov 2009 00:24
Subject: Re: Mix Static and dynamic Polymorphism
On 07/11/09 15:16, Jun wrote:
> Hello all,
> I just tried to mix static and dynamic polymorphism together.
> I've a vector to store all the elements, and the implementations > of elements are slightly different. I used Policy-based design:
> struct Interface{ > virtual void run(void) = 0; > virtual void execute(void) = 0; > };
> struct PolicyA{ > void execute(){ > // implementing Policy A; > } > };
> struct PolicyB{ > void execute(){ > // implementing Policy B; > } > };
> template<class Policy> > struct Base : public Policy, public Interface{ > void run(){ > // implementing base ; > } > }
> Anyway, those codes don't compile .... Anyone has some suggestions ? > Thank you in advance.
You could try inheritance chaining: struct Interface { virtual void run() = 0; virtual void execute() = 0; };
template<class Itf> struct PolicyA : Itf { void execute() { // implementing Policy A; } };
template<class Itf> struct PolicyB : Itf { void execute() { // implementing Policy B; } };
template<class Policy> struct Base : Policy { void run(){ // implementing base ; } };
typedef Base<PolicyA<Interface> > BaseA; typedef Base<PolicyB<Interface> > BaseB;
int main() { Interface const& a = BaseA(); Interface const& b = BaseB(); }
-- Max
[ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
You must
Sign in before you can post messages.
You do not have the permission required to post.
Newsgroups: comp.lang.c++.moderated
Follow-up To: comp.lang.c++.moderated
From:
Bart van Ingen Schenau <b... @ingen.ddns.info>
Date: Sun, 8 Nov 2009 09:52:22 CST
Local: Sun 8 Nov 2009 15:52
Subject: Re: Mix Static and dynamic Polymorphism
Jun wrote:
> Hello all,
> I just tried to mix static and dynamic polymorphism together.
> I've a vector to store all the elements, and the implementations > of elements are slightly different. I used Policy-based design:
> struct Interface{ > virtual void run(void) = 0; > virtual void execute(void) = 0; > };
> struct PolicyA{ > void execute(){ > // implementing Policy A; > } > };
Because PolicyA and Interface are two unrelated classes, the PolicyA::execute() member will never be considered and overrider for Interface::execute().
> struct PolicyB{ > void execute(){ > // implementing Policy B; > } > };
> template <class Policy> > struct Base : public Policy, public Interface{ > void run(){ > // implementing base ; > } > }
Base<T> remains an abstract class, because it does not have an overrider for the pure-virtual member execute() that was inherited from Interface. The most likely solutions are: 1. Add another member to Base, to connect the two inherited execute() functions: template <class Policy> void Base<Policy>::execute(){ Policy::execute(); } 2. Change the inheritance hierarchy such that the policy classes inherit from Interface.
> Anyway, those codes don't compile .... Anyone has some suggestions ? > Thank you in advance.
> Jun
Bart v Ingen Schenau -- a.c.l.l.c-c++ FAQ: http://www.comeaucomputing.com/learn/faq c.l.c FAQ: http://c-faq.com/ c.l.c++ FAQ: http://www.parashift.com/c++-faq-lite/ [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
You must
Sign in before you can post messages.
You do not have the permission required to post.