I
Icosahedron
I've been going through some old code trying to clean it up
and rearchitect it based on more modern C++ idioms. In the
old code I often used the Pimpl idiom on a class by class
basis, creating impl within a class as such:
a.h
class A {
...stuff...
struct AImpl;
AImpl* _impl;
};
a.cpp
struct AImpl {
...stuff...
};
After doing this in a number of classes, I thought it would
be "cool" to have a single Pimpl class that any class
wishing to define implementation specifics could inherit
from and then define a class specific Impl class as such:
pimpl.h
class Pimpl {
struct Impl;
boost::shared_ptr<Impl> _impl;
};
a.h
class A : public Pimpl {
... stuff ...
};
a.cpp
struct A::Impl {
... stuff ...
};
This compiled okay, but then I ran into an inheritance
problem (as many of you probably predicted just reading
this):
b.h
class B : public A, public Pimpl {
};
I could use virtual inheritance, but I want 2 impl classes,
one for A's implementation specific stuff and B's
implementation specific stuff. Of course, I thought I would
use templates to parameterize each one to give it a
different type so.
pimpl.h
template <typename T>
class Pimpl {
struct Impl;
boost::shared_ptr<Impl> _impl;
};
a.h
class A : public Pimpl<A> {
};
b.h
class B : public A, public Pimpl<B> {
};
Still, this has not quite gotten me where I want to be. I
would like to be able to have an A::_impl and a B::_impl
that I can distinguish inside of B based on the methods that
have to be called. The other is that, if possible, I wish
to refer to them as A::_impl rather than Pimpl<A>::_impl and
likewise for B.
Any suggestions? Is this whole thing just bad form? Should
I just stick with the original approach?
Jay
and rearchitect it based on more modern C++ idioms. In the
old code I often used the Pimpl idiom on a class by class
basis, creating impl within a class as such:
a.h
class A {
...stuff...
struct AImpl;
AImpl* _impl;
};
a.cpp
struct AImpl {
...stuff...
};
After doing this in a number of classes, I thought it would
be "cool" to have a single Pimpl class that any class
wishing to define implementation specifics could inherit
from and then define a class specific Impl class as such:
pimpl.h
class Pimpl {
struct Impl;
boost::shared_ptr<Impl> _impl;
};
a.h
class A : public Pimpl {
... stuff ...
};
a.cpp
struct A::Impl {
... stuff ...
};
This compiled okay, but then I ran into an inheritance
problem (as many of you probably predicted just reading
this):
b.h
class B : public A, public Pimpl {
};
I could use virtual inheritance, but I want 2 impl classes,
one for A's implementation specific stuff and B's
implementation specific stuff. Of course, I thought I would
use templates to parameterize each one to give it a
different type so.
pimpl.h
template <typename T>
class Pimpl {
struct Impl;
boost::shared_ptr<Impl> _impl;
};
a.h
class A : public Pimpl<A> {
};
b.h
class B : public A, public Pimpl<B> {
};
Still, this has not quite gotten me where I want to be. I
would like to be able to have an A::_impl and a B::_impl
that I can distinguish inside of B based on the methods that
have to be called. The other is that, if possible, I wish
to refer to them as A::_impl rather than Pimpl<A>::_impl and
likewise for B.
Any suggestions? Is this whole thing just bad form? Should
I just stick with the original approach?
Jay