this might sound useless/needless, but bear with me.. I'm just toying with some OO
i've been playing with some patterns to prohibit creation without hiding the the constructor + friend keyword (since i don't want an entire class exposed).
so i came up with this thing:
- big class that is allowed to instantiate the once i prohibit creation of
it has two classes :
- a private "real" class
- a public proxy class for interacting with the "real"
here's how it goes:
Code:
class SoupNazi
{
private: class HiddenSoup;
public: class SoupProxy;
public:
SoupNazi() { _real_soup = 0;}
~SoupNazi() {}
void makeSoup ();
void spillSoup();
SoupProxy gimmeProxy();
private:
HiddenSoup* _real_soup;
};
Code:
class SoupNazi::HiddenSoup
{
public:
HiddenSoup() { _val=0; }
~HiddenSoup() {}
void setVal ( int n ) {_val = n;}
int getVal ( ) const { return _val; }
private:
int _val;
};
Code:
class SoupNazi::SoupProxy
{
public:
SoupProxy ( SoupNazi::HiddenSoup* ref ) : _real(ref) { }
~SoupProxy () { }
const SoupNazi::HiddenSoup* const operator->() const {return _real;}
SoupNazi::HiddenSoup* const operator->() {return _real;}
private:
SoupNazi::HiddenSoup* const _real;
};
use:
Code:
...
SoupNazi nazi;
nazi.makeSoup();
SoupProxy proxy = nazi.gimmeProxy();
proxy->setVal(12);
std::cout<<proxy->getVal();
nazi.spillSoup();
...
so, the only mechanism i am missing is a way to know if the proxy actually has a real soup pointed at, but can keep running without trying to access member of a null pointer..
i hope i actually made this clear..
idea?