Google Mail Calendar Documents Reader Web more »
Recently Visited Groups | Help | Sign in
Google Groups Home
Mix Static and dynamic Polymorphism
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  6 messages - Collapse all  -  Translate all to Translated (View all originals)
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
 
From:
To:
Cc:
Follow-up To:
Add Cc | Add Follow-up to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers that you hear
 
Jun  
View profile   Translate to Translated (View Original)
 More options 7 Nov, 15:16
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! ]


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Neil Butterworth  
View profile   Translate to Translated (View Original)
 More options 7 Nov, 21:22
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

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! ]


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jun  
View profile   Translate to Translated (View Original)
 More options 8 Nov, 00:23
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:

--
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
tohava  
View profile   Translate to Translated (View Original)
 More options 8 Nov, 00:24
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! ]

    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Maxim Yegorushkin  
View profile   Translate to Translated (View Original)
 More options 8 Nov, 00:24
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:

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! ]


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Bart van Ingen Schenau  
View profile   Translate to Translated (View Original)
 More options 8 Nov, 15:52
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

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! ]


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google