objects hierarchy problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • vfunc@talktalk.net

    #1

    objects hierarchy problem

    Say you have object A and derived objects B and C, with B having a
    member x,
    and C having a member y. Then say we want an object D to also be
    derived from A but containing both members x and y. Is there a way to
    declare D using both B and C, like a "joint" object ? :o

  • Neelesh Bodas

    #2
    Re: objects hierarchy problem

    vfunc@talktalk. net wrote:[color=blue]
    > Say you have object A and derived objects B and C, with B having a
    > member x,
    > and C having a member y. Then say we want an object D to also be
    > derived from A but containing both members x and y. Is there a way to
    > declare D using both B and C, like a "joint" object ?[/color]


    You can use multiple inheritence


    class D: public B, public C
    {
    ....
    };

    If A is a virtual base class of B and C then there will be only one A
    subobject inside every instance of D. Otherwise there will be two
    A-subobjects inside any instance of D.

    Aside - AFAIK, we usually say "Class B is derived from A", not "Object
    B is derived from Object A". Object is an instance of a class.

    Comment

    • Stefan Näwe

      #3
      Re: objects hierarchy problem

      vfunc@talktalk. net schrieb:[color=blue]
      > Say you have object A and derived objects B and C, with B having a
      > member x,
      > and C having a member y. Then say we want an object D to also be
      > derived from A but containing both members x and y. Is there a way to
      > declare D using both B and C, like a "joint" object ? :o[/color]

      <nitpick on>
      You can not derive object B from object A.
      Only classes can derive from (or better: inherit) each other
      <nitpick off>

      Stefan
      --
      Stefan Naewe
      naewe.s_AT_atla s_DOT_de

      Comment

      • Ben Pope

        #4
        Re: objects hierarchy problem

        vfunc@talktalk. net wrote:[color=blue]
        > Say you have object A and derived objects B and C, with B having a
        > member x,
        > and C having a member y. Then say we want an object D to also be
        > derived from A but containing both members x and y. Is there a way to
        > declare D using both B and C, like a "joint" object ? :o[/color]

        An obvious question is why?

        Do you want to be able to replace B and C objects with a D? (i.e., use
        D polymorphically through pointers to B or C)

        Then multiple inheritance is the answer, but be careful. Read and
        understand the whole FAQ section on Multiple Inheritance.


        Do you want to be able to use the public interface of both B and C?
        Do you just want to make use of the implementation of both?

        In each case the following will suffice and you'll likely have less
        trouble than with mutliple inheritance.

        class D {
        B b_;
        C c_;
        }



        Ben Pope
        --
        I'm not just a number. To many, I'm known as a string...

        Comment

        Working...