Inheritance - style question

Discussion in 'C++' started by Paul N, Nov 20, 2009.

  1. Paul N

    Paul N Guest

    I'm using inheritance - this particular case is to provide a linked
    list of attributes of a piece of text (text colour, italic etc) so
    that different parts of it can be shown in different ways.

    class Attrib : public Listable {
    public:
    // stuff including where in the text the attribute starts to take
    effect
    };

    class Col_Attrib : public Attrib {
    public:
    COLORREF col;
    // plus more stuff
    };

    class Ink : public Col_Attrib {
    public:
    // stuff
    };

    class Paper : public Col_Attrib {
    public:
    // stuff
    };

    Now, if I include a constructor:

    Col_Attrib::Col_Attrib(COLORREF incol) : col(incol) { }

    then this doesn't, by itself, create constructors for Ink and Paper
    taking a COLORREF, does it?

    If I did want such constructors, would it be best to include a
    constructor as above and then do:

    Ink::Ink(COLORREF incol) : Col_Attrib(incol) { }

    or would it be better to do:

    Ink::Ink(COLORREF incol) : col(incol) { }

    directly? The first form seems more natural in terms of the hierarchy,
    but the second seems more efficient.

    Thanks for any advice.
    Paul.
    Paul N, Nov 20, 2009
    #1
    1. Advertising

  2. * Paul N:
    > I'm using inheritance - this particular case is to provide a linked
    > list of attributes of a piece of text (text colour, italic etc) so
    > that different parts of it can be shown in different ways.
    >
    > class Attrib : public Listable {
    > public:
    > // stuff including where in the text the attribute starts to take
    > effect
    > };


    Why not use a std::list.


    > class Col_Attrib : public Attrib {
    > public:
    > COLORREF col;
    > // plus more stuff
    > };
    >
    > class Ink : public Col_Attrib {
    > public:
    > // stuff
    > };
    >
    > class Paper : public Col_Attrib {
    > public:
    > // stuff
    > };
    >
    > Now, if I include a constructor:
    >
    > Col_Attrib::Col_Attrib(COLORREF incol) : col(incol) { }
    >
    > then this doesn't, by itself, create constructors for Ink and Paper
    > taking a COLORREF, does it?


    No.



    > If I did want such constructors, would it be best to include a
    > constructor as above and then do:
    >
    > Ink::Ink(COLORREF incol) : Col_Attrib(incol) { }
    >
    > or would it be better to do:
    >
    > Ink::Ink(COLORREF incol) : col(incol) { }
    >
    > directly? The first form seems more natural in terms of the hierarchy,
    > but the second seems more efficient.


    The second form wouldn't compile.


    Cheers & hth.,

    - Alf
    Alf P. Steinbach, Nov 20, 2009
    #2
    1. Advertising

  3. Paul N

    Paul N Guest

    On 20 Nov, 15:06, "Alf P. Steinbach" <> wrote:
    > * Paul N:
    >
    > > I'm using inheritance - this particular case is to provide a linked
    > > list of attributes of a piece of text (text colour, italic etc) so
    > > that different parts of it can be shown in different ways.

    >
    > > class Attrib : public Listable {
    > > public:
    > >   // stuff including where in the text the attribute starts to take
    > > effect
    > > };

    >
    > Why not use a std::list.


    Because I have already written a linked list class, which works
    perfectly well and which I understand how to use, whereas I don't know
    anything about templates. Though arguably it would be a good idea for
    me to find out :)

    > > class Col_Attrib : public Attrib {
    > > public:
    > >   COLORREF col;
    > >   // plus more stuff
    > > };

    >
    > > class Ink : public Col_Attrib {
    > > public:
    > >   // stuff
    > > };

    >
    > > class Paper : public Col_Attrib {
    > > public:
    > >   // stuff
    > > };

    >
    > > Now, if I include a constructor:

    >
    > > Col_Attrib::Col_Attrib(COLORREF incol) : col(incol) { }

    >
    > > then this doesn't, by itself, create constructors for Ink and Paper
    > > taking a COLORREF, does it?

    >
    > No.
    >
    > > If I did want such constructors, would it be best to include a
    > > constructor as above and then do:

    >
    > > Ink::Ink(COLORREF incol) : Col_Attrib(incol) { }

    >
    > > or would it be better to do:

    >
    > > Ink::Ink(COLORREF incol) : col(incol) { }

    >
    > > directly? The first form seems more natural in terms of the hierarchy,
    > > but the second seems more efficient.

    >
    > The second form wouldn't compile.


    Do you mean it won't compile *if I include the above-mentioned
    Col_Attrib constructor* or that it won't compile full stop? My
    question was intended to include this constructor only in the first
    alternative - sorry for the lack of clarity.

    Thanks.
    Paul.
    Paul N, Nov 20, 2009
    #3
  4. Paul N

    Paul N Guest

    On 21 Nov, 13:38, "Daniel T." <> wrote:
    > In article
    > <>,
    >  Paul N <> wrote:
    >
    >
    >
    >
    >
    > > I'm using inheritance - this particular case is to provide a linked
    > > list of attributes of a piece of text (text colour, italic etc) so
    > > that different parts of it can be shown in different ways.

    >
    > > class Attrib : public Listable {
    > > public:
    > >   // stuff including where in the text the attribute starts to take
    > > effect
    > > };

    >
    > > class Col_Attrib : public Attrib {
    > > public:
    > >   COLORREF col;
    > >   // plus more stuff
    > > };

    >
    > > class Ink : public Col_Attrib {
    > > public:
    > >   // stuff
    > > };

    >
    > > class Paper : public Col_Attrib {
    > > public:
    > >   // stuff
    > > };

    >
    > > Now, if I include a constructor:

    >
    > > Col_Attrib::Col_Attrib(COLORREF incol) : col(incol) { }

    >
    > > then this doesn't, by itself, create constructors for Ink and Paper
    > > taking a COLORREF, does it?

    >
    > No, it doesn't.
    >
    > > If I did want such constructors, would it be best to include a
    > > constructor as above and then do:

    >
    > > Ink::Ink(COLORREF incol) : Col_Attrib(incol) { }

    >
    > > or would it be better to do:

    >
    > > Ink::Ink(COLORREF incol) : col(incol) { }

    >
    > > directly? The first form seems more natural in terms of the hierarchy,
    > > but the second seems more efficient.

    >
    > The former. The latter won't compile. (yes, full stop.)
    >
    > Maybe if you included all the optional stuff that happens automatically
    > in the code, things might make more sense to you:
    >
    > Ink::Ink(COLORREF incol)
    >    : Col_Attrib()
    >    , col(incol)
    > { }
    >
    > The above doesn't work. You are trying to initialize 'col' but it was
    > already initialized in the Col_Attrib() constructor. The Col_Attrib()
    > constructor call happens, even if you don't include the code.
    > So you have to:
    >
    > Col_Attrib::Col_Attrib(COLORREF incol)
    >    : Attrib()
    >    , col(incol)
    > { }
    >
    > Ink::Ink(COLORREF incol)
    >    : Col_Attrib(incol)
    > { }
    >
    > Hope that helps.


    Thanks. Yes, it does help. I was getting confused between actual
    initialisation and "things that it would be nice to do in a
    constructor".
    Paul N, Nov 22, 2009
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. maxw_cc
    Replies:
    1
    Views:
    3,121
    Martijn van Steenbergen
    Dec 21, 2003
  2. Replies:
    1
    Views:
    781
    Bertilo Wennergren
    Nov 24, 2003
  3. Russell Silva

    Style question involving inheritance

    Russell Silva, Jul 26, 2003, in forum: C++
    Replies:
    1
    Views:
    320
    Russell Silva
    Jul 26, 2003
  4. cppsks
    Replies:
    0
    Views:
    806
    cppsks
    Oct 27, 2004
  5. Ken Varn
    Replies:
    0
    Views:
    430
    Ken Varn
    Apr 26, 2004
Loading...

Share This Page