initializing mutable class attributes

Alex Martelli aleaxit at yahoo.com
Fri Sep 3 10:06:32 CEST 2004


Dan Perl <dperl at rogers.com> wrote:

> "Alex Martelli" <aleaxit at yahoo.com> wrote in message
> news:1gjg9tm.14a3nqg36zs8fN%aleaxit at yahoo.com...
>      ........
> > Incidentally, FAR from your assertion that, in C++, "subclasses don't
> > need to know anything about how the superclass is implemented", reality
> > is just the other way 'round.  Subclasses have to be _intimately
> > familiar_ with key aspects of superclasses' implementation -- not just
   ...
> No, Alex.  Data members in a C++ subclass cannot conflict with data members
> in its superclass, public, protected, or private.  They are resolved by

Sorry, I didn't explain clearly the breakage example -- try this one,
which is as tiny as I can make it:

#include "Base.h"

int foo = 23;
class Derived: public Base {
    public:
        Derived() { foo = 45; }
};

Is this code correct?  Sure, it compiles and runs perfectly using Base
version 12.3.  Then Base comes out with version 12.4 and this does not
compile any more.  Why?  Because the new version of Base has added a
private member named foo.

Admittedly it breaks at compile-time, which is a bit better than
breaking at unittest-time as it would if Base had grown a _public_
member named foo (as the global foo doesn't get updated any more but
rather the per-instance one does).  But the point is: coupling between
superclasses and subclasses is STRONG.  To code a subclass, you can't
ignore what the superclass is doing, including some implementation
aspects -- much less of course ignore aspects of the superclass's public
interface, as you erroneously claimed.


Alex



More information about the Python-list mailing list