Re: Too many datamembers
On Nov 12, 3:04 pm, James Kanze <james.ka...@gmail.com> wrote:
On Nov 12, 11:42 am, Maxim Yegorushkin <maxim.yegorush...@gmail.com>
wrote:
On Nov 12, 3:11 am, sreeni <sreeni.h...@gmail.com> wrote:
I have a ordinary C++ class with lot of datamembers used
internally between methods of the same class.
Is there any other elegant way to maintain the private
datamembers.
The most elegant way is to hide the implementation details of
your class. All non-public members are an implementation
detail. You hide these by providing an abstract class /
interface and a factory function that creates an instance of
that interface.
That's one way. The more idiomatic way in C++ is the
compilation firewall idiom.
How do you tell which one is more idiomatic?
Both work, but neither is totally
without drawbacks. The abstract class/factory function, for
example, doesn't work if users have to be able to derive from
the interface.
Hm... It works for me - the venerable decorator design pattern:
#include <memory>
struct Interface
{
virtual ~Interface() = 0;
// idiomatic functions ;)))
virtual void foo() = 0;
virtual void bar() = 0;
};
std::auto_ptr<Interface> createInterfaceImplementation();
class DeriveFromInterface : public Interface
{
private:
std::auto_ptr<Interface> base_implementation_;
// disable these for simplicity
DeriveFromInterface(DeriveFromInterface const&);
DeriveFromInterface& operator=(DeriveFromInterface const&);
public:
void foo() { base_implementation_->foo(); }
void bar() { base_implementation_->bar(); }
DeriveFromInterface()
: base_implementation_(createInterfaceImplementation())
{}
};
--
Max