Boost logo

Boost :

Subject: Re: [boost] A Pimpl variant
From: Stewart, Robert (Robert.Stewart_at_[hidden])
Date: 2010-04-02 13:00:21


vicente.botet wrote:
>
> My experience is that I add more private member functions
> than private member data. I have explored a different variant
> 5. Put all private funtion and static data (but not
> instance data) into XImpl. XImpl needs only to store the back pointer.

[snip]

> class C {
> public:
> void f();
> void g();
> protected:
> // ...
> private:
> struct impl;
> friend struct impl;
> // the private part contains only instance data and virtual
> functions
> int priv;
> };
>
> * Identify the private functions and the private static data
> and put them on the implementation class
>
> struct C::impl {
> C::impl(C* thisC) : that(*thisC) {}
> void ifct() {
> that.priv = ... // use of private data
> that.g(); // call to a public function
> jfct(); // call another private function
> }
> C& that;
> static int icount;
> };
>
> * Prefix all the ex private function calls by impl(this) impl(this).ifct();
> * Access without constraints the impl static data impl::icount;

Wouldn't it be simpler to use static member functions?

struct C::impl
{
   static void
   ifct(C * _this)
   {
      _this->priv = ... // use private data
      _this->g(); // call public member function
      ...
   }
};

Using it from C member functions would be straightforward:

   void
   C::f()
   {
      impl::ifct(this);
      // compare: impl(this).ifct();
      ++impl::icount;
   }

The definition of impl is simpler in my version and invoking impl member functions is one (!) character shorter. Your version requires an implicit this argument from which "that" is found; mine makes your "that" the sole argument, so its potentially faster. For those that like "this->" in front of member access, mine even looks similar (in impl's member functions).

_____
Rob Stewart robert.stewart_at_[hidden]
Software Engineer, Core Software using std::disclaimer;
Susquehanna International Group, LLP http://www.sig.com

IMPORTANT: The information contained in this email and/or its attachments is confidential. If you are not the intended recipient, please notify the sender immediately by reply and immediately delete this message and all its attachments. Any review, use, reproduction, disclosure or dissemination of this message or any attachment by an unintended recipient is strictly prohibited. Neither this message nor any attachment is intended as or should be construed as an offer, solicitation or recommendation to buy or sell any security or other financial instrument. Neither the sender, his or her employer nor any of their respective affiliates makes any warranties as to the completeness or accuracy of any of the information contained herein or that this message or any of its attachments is free of viruses.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk