J
jason.cipriani
I'm writing a class where I want to keep a few details out of the
public header, but those details are only a small fraction of
everything in the class.
Normally I'd do this with pimpl or something, but I want to avoid the
inconvenience of coding and maintaining the entire pimpl abstraction.
I always get uncomfortable when I try a new design pattern though. My
question is: Is something like the example below in poor form? Is
there a better way?
So, say I originally have a class like this:
class MyClass {
public:
MyClass ();
~MyClass ();
void a ();
void b ();
void c ();
void d ();
private:
int x_;
int y_;
};
And I only want to separate part of it. So I do this (I've inlined
function definitions to keep the example brief but note that, of
course, the actual source code is split into appropriate headers and
source files):
class MyClass {
public:
MyClass () { impl_ = new MyClassPartialImpl(this); }
~MyClass () { delete impl_; }
void a ();
void b ();
void c () { impl_->c(); }
void d () { impl_->d(); }
private:
int x_;
friend MyClassPartialImpl;
MyClassPartialImpl *impl_;
};
class MyClassPartialImpl {
public:
explicit MyClassPartialImpl (MyClass *owner) : owner_(owner) { }
~MyClassPartialImpl ();
void c ();
void d ();
private:
MyClass *owner_;
int y_;
};
Here, I've moved the functions c() and d(), and the data y_, into
MyClassPartialImpl. MyClassPartialImpl still has access to the data
x_, and the functions a() and b(), via owner_.
Am I doing something sloppy? Should I go all the way and put
everything in MyClassPartialImpl (and why)? Is there a more
traditional way to do what I'm trying to do (hide some details from
the public but keep the implementation convenient)?
Sorry, if this is a vague or silly question.
Thanks,
Jason
public header, but those details are only a small fraction of
everything in the class.
Normally I'd do this with pimpl or something, but I want to avoid the
inconvenience of coding and maintaining the entire pimpl abstraction.
I always get uncomfortable when I try a new design pattern though. My
question is: Is something like the example below in poor form? Is
there a better way?
So, say I originally have a class like this:
class MyClass {
public:
MyClass ();
~MyClass ();
void a ();
void b ();
void c ();
void d ();
private:
int x_;
int y_;
};
And I only want to separate part of it. So I do this (I've inlined
function definitions to keep the example brief but note that, of
course, the actual source code is split into appropriate headers and
source files):
class MyClass {
public:
MyClass () { impl_ = new MyClassPartialImpl(this); }
~MyClass () { delete impl_; }
void a ();
void b ();
void c () { impl_->c(); }
void d () { impl_->d(); }
private:
int x_;
friend MyClassPartialImpl;
MyClassPartialImpl *impl_;
};
class MyClassPartialImpl {
public:
explicit MyClassPartialImpl (MyClass *owner) : owner_(owner) { }
~MyClassPartialImpl ();
void c ();
void d ();
private:
MyClass *owner_;
int y_;
};
Here, I've moved the functions c() and d(), and the data y_, into
MyClassPartialImpl. MyClassPartialImpl still has access to the data
x_, and the functions a() and b(), via owner_.
Am I doing something sloppy? Should I go all the way and put
everything in MyClassPartialImpl (and why)? Is there a more
traditional way to do what I'm trying to do (hide some details from
the public but keep the implementation convenient)?
Sorry, if this is a vague or silly question.
Thanks,
Jason