Moving private functions to opaque inner
What triggered me to post it was that I saw in comp.std.c++ a
strange "Proposal: Compilation-unit scoped private member functions"
that received some positive feedback, so may be there is some
market for idiom like that? Idiom (sorry, I do not have name):
// A.hpp
class A
{
public:
// public interface
int PublicGetter() const;
int PublicSetter(int param);
private:
// the place for private member functions,
// so (sometimes annoying) bloat is removed
class P;
// only virtuals constructors and operators here
// ... not relevant for example
// member data not moved, this isn't "pimpl"
int data;
};
// A.cpp
class A::P
{
public:
// private functions of A hidden parameter exposed,
// 7 chars of bloat per signature compare with:
// "int A::PrivateGetter() const"
static int PrivateGetter(A const&x)
{
// 2 chars of bloat per access
return x.data;
}
// same here
static int PrivateSetter(A&a,int param)
{
x.data += param;
return x.data;
}
};
int A::PublicGetter() const
{
// 9 chars of bloat per private call
return P::PrivateGetter(*this,param);
}
int A::PublicSetter(int param)
{
// same here
return P::PrivateSetter(*this,param);
}
No differences in efficiency.
Removes most private functions from header.
Makes private calls more visible in code.
Changes in implementation modify header less likely.
May be that ... I do not see something obvious?
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]