Re: Google C++ Style Guide
Ozan Ayy?ce wrote:
Torsten Robitzki <MyFirstname@robitzki.de>:
The (multiple) inheritance rules seems a little bit odd to me. Why
should one use only public inheritance? I have some observers here and
they attach to the observed object in the c'tor and detach in the d'tor.
They have to derive from a certain observer interface, but there is no
reason, why they should do this public.
Can you elaborate on why you can not use the observer class as a
private or protected member?
I didn't said that I can not use the observer as private member. What I
said was: why using public inheritance when private inheritance will be
sufficient?
The observer pattern is used to decouple a data-like object from othere
objects that need to know its state and must be informed about state
changes. The benefit of using the observer pattern is, that the observed
object do not need to know the concret objects that are observing the
observed object.
So using the observer as members of the data object will couple them
very strong together.
Example:
class print_job;
class printer_queue;
class observer
{
public:
virtual void new_job(const printer_queue&) = 0;
};
class printer_queue
{
public:
void attach(observer&);
void detach(const observer&);
void print(const print_job&);
};
class display : private observer, boost::noncopyable
{
public:
explicit display(printer_queue& q)
: queue_(q)
{
q.attach(*this);
}
~display()
{
queue_.detach(*this);
}
private:
virtual void new_job(const printer_queue&);
printer_queue& queue_;
};
Display can inherit privatly the observer interface. If the inheritance
would be public, the new_job() function could be called directly with an
othere printer_queue.
best regards
Torsten
--
kostenlose Wirtschaftssimulation: http://www.financial-rumors.de
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]