Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #7572

Deriving from concrete types like std::list

From Urs Thuermann <urs@isnogud.escape.de>
Newsgroups comp.lang.c++
Subject Deriving from concrete types like std::list
Date 2011-07-07 14:02 +0200
Organization escape e.V. - Verein zur Foerderung der Datenkommunikation
Message-ID <ygfd3hm1eor.fsf@janus.isnogud.escape.de> (permalink)

Show all headers | View raw


In the 3rd edition of "The C++ Programming Language", Stroustrup
writes in section 25.2 about concrete types

        "Classes such as vector, list [...] are concrete [...]
         Also, [...] none are intended as a base for derivation."

and two pages later

        "A class that does this well is rarely a good candidate for
         the creation of different but related classes through public
         derivation."

Some of the following arguments seems valid, but still I don't see why
I shouldn't derive a new class from list.

As a C++ learning project I'm re-implementing a simple interpreter I
wrote in C almost 20 years ago.

I have an abstract base class Stmt from which I derive several
concrete statement types, one of which is the StmtList.  I want the
StmtList to inherit from Stmt and from list <Stmt *>, so I can call
push_back() on it in the parser or iterate through it to execute the
statement list.  Currently, I have something like this, which works
perfectly and, IMHO, looks quite nice:

        class Stmt {
        public:
                virtual void exec() = 0;
        };

        class WhileStmt : public Stmt {
                Expr *cond;
                Stmt *stmt;
        public:
                virtual void exec() {
                        while (cond->eval())
                                stmt->exec();
                }
        };

        class StmtList : public Stmt, public std::list <Stmt *> {
        public:
                virtual void exec() {
                        const_iterator it;
                        for (it = begin(); it != end(); ++it)
                                (*it)->exec();
                }
        };

        // in the parser
                stmt_list->push_back(stmt);

Is this, according to Stroustrup, the wrong approach?  Instead of
deriving I could have a member of type list <Stmt *> in class StmtList
but iterating would look more cumbersome and I had to implement my own
push_back() that calls push_back() for that list member, etc.  After
all, a statement list *is* a list.  Shouldn't I express this
relationship by derivation?

urs

Back to comp.lang.c++ | Previous | Next — Next in thread | Find similar | Unroll thread


Thread

Deriving from concrete types like std::list Urs Thuermann <urs@isnogud.escape.de> - 2011-07-07 14:02 +0200
  Re: Deriving from concrete types like std::list Leigh Johnston <leigh@i42.co.uk> - 2011-07-07 14:04 +0100
  Re: Deriving from concrete types like std::list Alexander Bartolich <alexander.bartolich@gmx.at> - 2011-07-07 13:42 +0000
    Re: Deriving from concrete types like std::list Kai-Uwe Bux <jkherciueh@gmx.net> - 2011-07-07 22:23 +0200
      Re: Deriving from concrete types like std::list Alexander Bartolich <alexander.bartolich@gmx.at> - 2011-07-07 22:04 +0000
        Re: Deriving from concrete types like std::list Kai-Uwe Bux <jkherciueh@gmx.net> - 2011-07-08 01:16 +0200
    Re: Deriving from concrete types like std::list red floyd <redfloyd@gmail.com> - 2011-07-08 11:23 -0700
      Re: Deriving from concrete types like std::list "Balog Pal" <pasa@lib.hu> - 2011-07-08 20:30 +0200
        Re: Deriving from concrete types like std::list "Alf P. Steinbach /Usenet" <alf.p.steinbach+usenet@gmail.com> - 2011-07-08 21:16 +0200
        Re: Deriving from concrete types like std::list red floyd <redfloyd@gmail.com> - 2011-07-08 14:53 -0700
          Re: Deriving from concrete types like std::list "Balog Pal" <pasa@lib.hu> - 2011-07-09 10:33 +0200
            Re: Deriving from concrete types like std::list Leigh Johnston <leigh@i42.co.uk> - 2011-07-09 10:59 +0100
  Re: Deriving from concrete types like std::list "Bo Persson" <bop@gmb.dk> - 2011-07-07 19:12 +0200
  Re: Deriving from concrete types like std::list "MikeP" <mp011011@some.org> - 2011-07-07 15:51 -0500

csiph-web