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


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

Re: Deriving from concrete types like std::list

From Kai-Uwe Bux <jkherciueh@gmx.net>
Newsgroups comp.lang.c++
Subject Re: Deriving from concrete types like std::list
Followup-To comp.lang.c++
Date 2011-07-07 22:23 +0200
Organization University of Bielefeld
Message-ID <iv54kp$mu$1@hoshi.visyn.net> (permalink)
References <ygfd3hm1eor.fsf@janus.isnogud.escape.de> <iv4d4f$tkb$1@news.albasani.net>

Followups directed to: comp.lang.c++

Show all headers | View raw


Alexander Bartolich wrote:

> Urs Thuermann wrote:
>> [...]
>>         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?
> 
> Oh, my. There are two arguments against this.
> 
> First, the nice one: by deriving from std::list you make the interface
> of StmtList extremly large. This gives users of StmtList a lot of ways
> to mess with its content. Which means that you cannot deduce the behav-
> iour of the class from its methods alone. You always have to check that
> nobody out there does nothing stupid with its instances. This totally
> defeats the reason we are doing OOP.

I do not understand the phrase "deduce the behavior of the class from its 
methods alone". What I can  see is that the implementation via derivation 
from std::list< Stmt* > prevents StmtList from imposing any sort of 
restriction on the content of the list of statements that is considered 
allowed: client code can append and erase in the list at will. What I fail 
to see is why that is wrong.

 
> The second argument is not so nice: your code does not actually work.
> That is, your claim that »a statement list *is* a list« is false.
> Hint: how many methods of std::list are declared virtual?

No methods in std::list< Stmt* > are virtual. But how would that matter? In 
the code shown, there is no attempt at overriding any of them.


Best,

Kai-Uwe Bux

Back to comp.lang.c++ | Previous | Next — Previous in thread | 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