I have something like the following:
// foo.h:
class foo {
public:
foo();
~foo();
// note: the param type repetition here is only incidental, assume the
// functions can't easily be made to share type signatures
void bar(a b, c d);
void baz(a b, c d, e f, g h, i j);
void quux(a b, c d, e f, g h, i j);
private:
class impl;
impl *m_pimpl;
}
Then:
// foo.cpp:
class foo::impl {
public:
void bar(a b, c d);
void baz(a b, c d, e f, g h, i j);
void quux(a b, c d, e f, g h, i j);
private:
// lots of private state, helper functions, etc.
};
void foo::impl::bar(a b, c d) { ... }
void foo::impl::baz(a b, c d, e f, g h, i j) { ... }
void foo::impl::quux(a b, c d, e f, g h, i j) { ... }
foo::foo() : m_pimpl(new impl()) { }
foo::~foo() { delete m_pimpl; m_pimpl = NULL; }
void foo::bar(a b, c d) {
return m_pimpl->bar(b, d);
}
void foo::baz(a b, c d, e f, g h, i j) {
return m_pimpl->baz(b, d, f, h, j)
}
void foo::quux(a b, c d, e f, g h, i j) {
return m_pimpl->quux(b, d, f, h, j);
}
There's a lot of repetition of bar
, baz
, and quux
here:
- Once in
foo
's declaration - Once in
foo::impl
's declaration - Once in
foo::impl
's definitions - Twice in
foo
's definitions: once forfoo
's function param list and again to call the correspondingfoo::impl
functions.
For each of these except the last I have to write out the entire parameter list, whose types can be more involved.
What's the best way, if any, to reduce the repetition here? One easy one is to inline the definition of the foo::impl
public functions, but is there anything besides that outside of designing the classes differently to have fewer public functions?
ECX
tom_pimpl
andjmp
to appropriate function... – Devlinm_pimpl->
. That is an option though, that'll reduce it by a good amount. – Asper