Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ Moderated > Re: Differentia...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 3 of 9 Topic 9505 of 9775
Post > Topic >>

Re: Differentiating pimpl idiom classes in c++

by Carl Barron <cbarron413@[EMAIL PROTECTED] > Apr 17, 2008 at 04:13 AM

In article
<38f73473-466e-4baf-8358-add3121b2ae0@[EMAIL PROTECTED]
>,
Fejimush <grahamreitz@[EMAIL PROTECTED]
> wrote:

> What are good strategies for selecting, either at run-time or compile
> time, various pimpl'ed implementations?  Also, retaining the ability
> to switch implementations without recompiling.
> 
   without recompiling most likely runtime polymorhism.  If you don't
want to add classes derived from the base at will the fact they are all
derived from a common base can also be hidden by not exposing them
to users.  If you need to add pimples at will, I think its a design
problem.  If you have two or three policies to be choosen at run time
provide a function boost::shared_ptr<Pimpl> create_pimple(/* some arg
to say which */); hidden from user and use it to get the proper derived
class ptr in the shared_ptr<Pimple>  I don't see the problem,
//  foo header foo.hpp
#include <boost/shared_ptr.hpp>
    class Foo
    {
       class Pimpl;
       boost::shared_ptr<Pimpl> pimpl;
    public:
       explicit Foo(int n=1);
       void something();
    // ...
    };
//  implementation
#include "foo.hpp"
    struct Foo::Pimple
    {
       // base class with virt funcs.
    };

    namespace
    {
       struct Pimpl_1:Pimpl { /**/};
       struct Pimpl_2:Pimpl {/**/};
       // ,,,
    }

    Foo:Foo(int n)
    {
       switch(n)
       {
       case 1:pimpl = boost::shared_ptr<Pimpl_1>(new Pimp_1); break;
       case 2:pimpl = boost::shared_ptr<Pimpl_2>(new Pimpl_2);break;
       // ...
       default:pimpl = boost::shared_ptr<Pimpl_1>(new Pimpl_1); break;
       }
    };

    void Foo::something()
    {
       access data thru pimpl's virt. funcs.
    }
    // ...

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




 9 Posts in Topic:
Differentiating pimpl idiom classes in c++
Fejimush <grahamreitz@  2008-04-16 11:36:38 
Re: Differentiating pimpl idiom classes in c++
Tony Delroy <tony_in_d  2008-04-17 03:55:28 
Re: Differentiating pimpl idiom classes in c++
Carl Barron <cbarron41  2008-04-17 04:13:14 
Re: Differentiating pimpl idiom classes in c++
Fejimush <grahamreitz@  2008-04-17 12:03:38 
Re: Differentiating pimpl idiom classes in c++
Carl Barron <cbarron41  2008-04-17 19:35:32 
Re: Differentiating pimpl idiom classes in c++
Tony Delroy <tony_in_d  2008-04-18 06:13:17 
Re: Differentiating pimpl idiom classes in c++
Fejimush <grahamreitz@  2008-04-19 02:01:53 
Re: Differentiating pimpl idiom classes in c++
Tony Delroy <tony_in_d  2008-04-20 21:04:20 
Re: Differentiating pimpl idiom classes in c++
marlow.andrew@[EMAIL PROT  2008-04-21 20:28:04 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Tue Jul 8 23:40:58 CDT 2008.