Selective Inheritance?

Discussion in 'C++' started by Suraj, Sep 20, 2010.

  1. Suraj

    Suraj Guest

    hello,

    I have a base class (Base) with a lot of member variables. The size of
    this class is 16bytes.

    I need to implement another class (Derived) with partially different
    functionality. If I derive Derived from Base, all members of Base
    would be available in Derived.

    This is unnecessary. In fact, the actual members that are needed just
    constitute of 8bytes of data.

    I need to develop a mechanism to selectively inherit member variables
    and functions from Base into Derived.

    Note that any change in Base isn't possible.

    1. Is it possible?
    2. How?

    Thanks
    Suraj
     
    Suraj, Sep 20, 2010
    #1
    1. Advertising

  2. Suraj

    Suraj Guest

    On Sep 21, 2:39 am, Ian Collins <> wrote:
    > On 09/21/10 09:35 AM, Suraj wrote:
    >
    > > hello,

    >
    > > I have a base class (Base) with a lot of member variables. The size of
    > > this class is 16bytes.

    >
    > > I need to implement another class (Derived) with partially different
    > > functionality. If I derive Derived from Base, all members of Base
    > > would be available in Derived.

    >
    > > This is unnecessary. In fact, the actual members that are needed just
    > > constitute of 8bytes of data.

    >
    > It may be unnecessary, but does it cause a problem?
    >
    > > I need to develop a mechanism to selectively inherit member variables
    > > and functions from Base into Derived.

    >
    > > Note that any change in Base isn't possible.

    >
    > Then you are out of luck.
    >
    > > 1. Is it possible?

    >
    > No.  You can't remove members from a class.  The only option is to
    > create a new base class with the required subset.
    >
    > --
    > Ian Collins


    The size does cause a problem. I am talking of a system with a small
    amount of user memory.
    The no. of objects that can be instantiated can be a big number. Say,
    1024 which is considered as a valid and a pretty lightly loaded
    configuration.
    This is a wastage of 8192 bytes which can be used for other more
    important objects of an average size of say 200bytes.

    Thanks.
     
    Suraj, Sep 20, 2010
    #2
    1. Advertising

  3. Suraj

    Fred Guest

    On Sep 20, 2:53 pm, Suraj <> wrote:
    > On Sep 21, 2:39 am, Ian Collins <> wrote:
    >
    >
    >
    >
    >
    > > On 09/21/10 09:35 AM, Suraj wrote:

    >
    > > > hello,

    >
    > > > I have a base class (Base) with a lot of member variables. The size of
    > > > this class is 16bytes.

    >
    > > > I need to implement another class (Derived) with partially different
    > > > functionality. If I derive Derived from Base, all members of Base
    > > > would be available in Derived.

    >
    > > > This is unnecessary. In fact, the actual members that are needed just
    > > > constitute of 8bytes of data.

    >
    > > It may be unnecessary, but does it cause a problem?

    >
    > > > I need to develop a mechanism to selectively inherit member variables
    > > > and functions from Base into Derived.

    >
    > > > Note that any change in Base isn't possible.

    >
    > > Then you are out of luck.

    >
    > > > 1. Is it possible?

    >
    > > No.  You can't remove members from a class.  The only option is to
    > > create a new base class with the required subset.

    >
    > > --
    > > Ian Collins

    >
    > The size does cause a problem. I am talking of a system with a small
    > amount of user memory.
    > The no. of objects that can be instantiated can be a big number. Say,
    > 1024 which is considered as a valid and a pretty lightly loaded
    > configuration.
    > This is a wastage of 8192 bytes which can be used for other more
    > important objects of an average size of say 200bytes.
    >
    > Thanks.- Hide quoted text -
    >
    > - Show quoted text -


    If your derived class does not need most of the base calss, then it is
    not really a subclass of that base.

    Isolate the things that are needed in both classes, and put those
    things in a new "NewBase" class.
    Then your original base class becomes a subclass of NewBase, and your
    original derived class is also a subclass of NewBase.
    --
    Fred K
     
    Fred, Sep 20, 2010
    #3
  4. On Sep 20, 5:35 pm, Suraj <> wrote:
    > hello,
    >
    > I need to develop a mechanism to selectively inherit member variables
    > and functions from Base into Derived.


    class Base {int a;};

    class A: public Base {int b;};
    class B: public C {int c;};
    class D: public D {int d;};
    ....
    class Derived-X: public X; //include all members til class X

    I also posed myself this problem when I learned C++ and it IS the C++
    problem. Most classes show the same concern as to reduce size for it
    and its possible derivations. Some solutions can be quite complex and
    are not worth the while when you consider pointers vs architecture
    complexity. I think that was the reason for virtual inheritance and
    templates to make it into the language... but it only makes sense for
    functionality bundles, not to bundle together data types, for this
    last case it is much better to make as many structs suitably named as
    data type combinations are relevant...

    Danilo J Bonsignore
     
    Fabrizio J Bonsignore, Sep 21, 2010
    #4
  5. Suraj

    Öö Tiib Guest

    On Sep 21, 2:40 pm, "Daniel T." <> wrote:
    > Fabrizio J Bonsignore <> wrote:
    >
    >
    >
    >
    >
    > > On Sep 20, 5:35 pm, Suraj <> wrote:

    >
    > > > hello,

    >
    > > > I need to develop a mechanism to selectively inherit member variables
    > > > and functions from Base into Derived.

    >
    > > class Base {int a;};

    >
    > > class A: public Base {int b;};
    > > class B: public C {int c;};
    > > class D: public D {int d;};
    > > ...
    > > class Derived-X: public X; //include all members til class X

    >
    > > I also posed myself this problem when I learned C++ and it IS the C++
    > > problem. Most classes show the same concern as to reduce size for it
    > > and its possible derivations. Some solutions can be quite complex and
    > > are not worth the while when you consider pointers vs architecture
    > > complexity. I think that was the reason for virtual inheritance and
    > > templates to make it into the language... but it only makes sense for
    > > functionality bundles, not to bundle together data types, for this
    > > last case it is much better to make as many structs suitably named as
    > > data type combinations are relevant...

    >
    > One of the prime reasons to make a class is to ensure that an invariant
    > between two or more variables is never broken, the class becomes the
    > guardian of the variables so it can restrict operations on them to keep
    > the invariant. As such, it makes little sense to have a class with only
    > one variable in it, unless you specifically want to restrict the range
    > of that variable.


    I suspect that you forget number of senses there.

    Some that i see to be used in several places:
    * encapsulating/wrapping that variable to make it compatible with some
    interface.
    * hiding information (even type of) that variable.
    * restricting transitions between the values of that variable.
    * keeping invariant with external single state (variable being id in
    some, possibly hidden, storage).
     
    Öö Tiib, Sep 21, 2010
    #5
  6. Suraj

    Goran Guest

    On Sep 20, 11:35 pm, Suraj <> wrote:
    > hello,
    >
    > I have a base class (Base) with a lot of member variables. The size of
    > this class is 16bytes.
    >
    > I need to implement another class (Derived) with partially different
    > functionality. If I derive Derived from Base, all members of Base
    > would be available in Derived.
    >
    > This is unnecessary. In fact, the actual members that are needed just
    > constitute of 8bytes of data.
    >
    > I need to develop a mechanism to selectively inherit member variables
    > and functions from Base into Derived.
    >
    > Note that any change in Base isn't possible.
    >
    > 1. Is it possible?


    Question is already answered, I just wanted to add "world be worse
    off if it was possible". When you derive, derived class IS A base
    class. That's the expected behavior, that is the very purpose of
    derivation. You derive a class, pass it to any old code that expects
    base, and it still works correctly. What you want is to derive, and
    then break IS-A relationship. That's just wrong.

    Also, given that you want to cut parts of Base, it looks like you know
    already what Base does and how (otherwise, you would not know what
    members you can leave out). If so, you can just as well duplicate
    desired functionality in another, non-related class.

    And finally, if you can carve out parts of Base, chances are that Base
    already has too much responsibilities, which is a well-known design
    smell.

    Goran.
     
    Goran, Sep 21, 2010
    #6
  7. Suraj

    TJorgenson Guest

    > Question  is already answered, I just wanted to add "world be worse
    > off if it was possible". When you derive, derived class IS A base
    > class.


    Agreed, with a minor correction...

    When you derive _using public inheritance_, derived class IS A base
    class.

    Tony
     
    TJorgenson, Sep 21, 2010
    #7
  8. Suraj

    Daniel Pitts Guest

    On 9/20/2010 2:35 PM, Suraj wrote:
    > hello,
    >
    > I have a base class (Base) with a lot of member variables. The size of
    > this class is 16bytes.
    >
    > I need to implement another class (Derived) with partially different
    > functionality. If I derive Derived from Base, all members of Base
    > would be available in Derived.
    >
    > This is unnecessary. In fact, the actual members that are needed just
    > constitute of 8bytes of data.

    How about creating a RealBase, which has only the 8 bytes of data, then
    making Base derive from RealBase and Derived also from RealBase.

    Derived classes should only add functionality and invariants, not remove
    them.
    > I need to develop a mechanism to selectively inherit member variables
    > and functions from Base into Derived.

    I doubt you do.
    >
    > Note that any change in Base isn't possible.

    I doubt that's true. It may be undesirable; it may even be impractical.
    Neither of those are the same as "not possible". Why don't you think
    changing Base is possible? Perhaps someone on here can suggest a way to
    do so that doesn't interfere with whatever you think prevents you from
    changing Base.
    >
    > 1. Is it possible?

    Yes.
    > 2. How?

    See above.

    --
    Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
     
    Daniel Pitts, Sep 21, 2010
    #8
  9. Suraj

    Suraj Guest

    On Sep 22, 1:27 am, Daniel Pitts
    <> wrote:
    > On 9/20/2010 2:35 PM, Suraj wrote:> hello,
    >
    > > I have a base class (Base) with a lot of member variables. The size of
    > > this class is 16bytes.

    >
    > > I need to implement another class (Derived) with partially different
    > > functionality. If I derive Derived from Base, all members of Base
    > > would be available in Derived.

    >
    > > This is unnecessary. In fact, the actual members that are needed just
    > > constitute of 8bytes of data.

    >
    > How about creating a RealBase, which has only the 8 bytes of data, then
    > making Base derive from RealBase and Derived also from RealBase.
    >
    > Derived classes should only add functionality and invariants, not remove
    > them.> I need to develop a mechanism to selectively inherit member variables
    > > and functions from Base into Derived.

    > I doubt you do.
    >
    > > Note that any change in Base isn't possible.

    >
    > I doubt that's true.  It may be undesirable; it may even be impractical..
    >   Neither of those are the same as "not possible".  Why don't you think
    > changing Base is possible? Perhaps someone on here can suggest a way to
    > do so that doesn't interfere with whatever you think prevents you from
    > changing Base.
    >
    > > 1. Is it possible?

    > Yes.
    > > 2. How?

    >
    > See above.
    >
    > --
    > Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>


    Well, I agree that it is possible but undesirable due to other
    factors. In any case, changing Base isn't an option so, the
    implementation will have to live through it.

    Perhaps what I was looking for is a "IS A" relation but a "lesser" IS
    A rather than "greater" IS A which is what extending a class achieves.

    Thanks all
     
    Suraj, Sep 21, 2010
    #9
  10. Suraj

    Öö Tiib Guest

    On 22 sept, 03:45, "Daniel T." <> wrote:
    > Öö Tiib <> wrote:
    >
    > > I suspect that you forget number of senses there.

    >
    > > Some that i see to be used in several places:
    > > * encapsulating/wrapping that variable to make it compatible with some
    > > interface.
    > > * hiding information (even type of) that variable.
    > > * restricting transitions between the values of that variable.
    > > * keeping invariant with external single state (variable being id in
    > > some, possibly hidden, storage).

    >
    > The point is that classes with single member variables should be quite
    > rare.


    With that i disagreed. Libraries have often such classes in interface
    for hiding information and improving compilation speed.

    > To have a chain of inheritance from A to X, each with a single
    > variable is likely silly.


    With that i fully agree. Long chains (tall forest) of inheritance add
    unneeded complexity, hit performance (by using yo-yo of virtuals) and
    make it hard to maintain.
     
    Öö Tiib, Sep 22, 2010
    #10
  11. Suraj

    Jeff Flinn Guest

    Suraj wrote:
    > On Sep 22, 1:27 am, Daniel Pitts
    > <> wrote:
    >> On 9/20/2010 2:35 PM, Suraj wrote:> hello,
    >>
    >>> I have a base class (Base) with a lot of member variables. The size of
    >>> this class is 16bytes.
    >>> I need to implement another class (Derived) with partially different
    >>> functionality. If I derive Derived from Base, all members of Base
    >>> would be available in Derived.
    >>> This is unnecessary. In fact, the actual members that are needed just
    >>> constitute of 8bytes of data.

    >> How about creating a RealBase, which has only the 8 bytes of data, then
    >> making Base derive from RealBase and Derived also from RealBase.
    >>
    >> Derived classes should only add functionality and invariants, not remove
    >> them.> I need to develop a mechanism to selectively inherit member variables
    >>> and functions from Base into Derived.

    >> I doubt you do.
    >>
    >>> Note that any change in Base isn't possible.

    >> I doubt that's true. It may be undesirable; it may even be impractical.
    >> Neither of those are the same as "not possible". Why don't you think
    >> changing Base is possible? Perhaps someone on here can suggest a way to
    >> do so that doesn't interfere with whatever you think prevents you from
    >> changing Base.
    >>
    >>> 1. Is it possible?

    >> Yes.
    >>> 2. How?

    >> See above.
    >>
    >> --
    >> Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>

    >
    > Well, I agree that it is possible but undesirable due to other
    > factors. In any case, changing Base isn't an option so, the
    > implementation will have to live through it.
    >
    > Perhaps what I was looking for is a "IS A" relation but a "lesser" IS
    > A rather than "greater" IS A which is what extending a class achieves.


    I think what you have is a "I want it to be" relation.

    Jeff
     
    Jeff Flinn, Sep 22, 2010
    #11
    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. Bobby Maul
    Replies:
    2
    Views:
    484
    Eddie Dodd
    May 27, 2004
  2. =?Utf-8?B?TGlsbGlhbg==?=

    Selective DataGrid

    =?Utf-8?B?TGlsbGlhbg==?=, Jun 2, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    384
    Marina
    Jun 2, 2004
  3. =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=

    selective validation on specific buttons only

    =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=, Mar 22, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    415
    =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=
    Mar 22, 2005
  4. =?Utf-8?B?U29saXR1cw==?=

    Selective databinding

    =?Utf-8?B?U29saXR1cw==?=, Oct 10, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    437
    Karl Seguin
    Oct 10, 2005
  5. yasaswi

    selective page access

    yasaswi, Feb 3, 2005, in forum: Java
    Replies:
    2
    Views:
    328
    yasaswi
    Feb 4, 2005
Loading...

Share This Page