base class initialization

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Allen

    base class initialization

    Hi all,

    I have a derived class that inherits from two base classes. One of the
    base class constructors takes as input a pointer that is initialized in the
    other base class. How can I make sure the base classes are properly
    initialized with the data they need when I call the derived class'
    constructor?
    --

    Best wishes,
    Allen

    No SPAM in my email !!




  • John Harrison

    #2
    Re: base class initialization


    "Allen" <allen-terri-ng@att.SPAM.net > wrote in message
    news:B_nWa.8296 0$0v4.5532056@b gtnsc04-news.ops.worldn et.att.net...[color=blue]
    > Hi all,
    >
    > I have a derived class that inherits from two base classes. One of[/color]
    the[color=blue]
    > base class constructors takes as input a pointer that is initialized in[/color]
    the[color=blue]
    > other base class. How can I make sure the base classes are properly
    > initialized with the data they need when I call the derived class'
    > constructor?
    > --
    >
    > Best wishes,
    > Allen
    >[/color]

    Sounds like bad design, if the two bases classes are so closely linked
    shouldn't they be a single class. Or maybe you should make the dependent
    class a member of the derived class.

    But if you write

    class D : public B1, public B2
    {
    D();
    };

    Then it is guaranteed that B1 will be constructed before B2. So you can
    write something like this

    D::D() : B2(get_ptr_from _b1())
    {
    }

    john


    Comment

    • Allen

      #3
      Re: base class initialization

      Hi John,

      "John Harrison" <john_andronicu s@hotmail.com> wrote in message
      news:bgd2tg$mtt ad$1@ID-196037.news.uni-berlin.de...[color=blue]
      >
      > "Allen" <allen-terri-ng@att.SPAM.net > wrote in message
      > news:B_nWa.8296 0$0v4.5532056@b gtnsc04-news.ops.worldn et.att.net...[color=green]
      > > Hi all,
      > >
      > > I have a derived class that inherits from two base classes. One of[/color]
      > the[color=green]
      > > base class constructors takes as input a pointer that is initialized in[/color]
      > the[color=green]
      > > other base class. How can I make sure the base classes are properly
      > > initialized with the data they need when I call the derived class'
      > > constructor?
      > > --
      > >
      > > Best wishes,
      > > Allen
      > >[/color]
      >
      > Sounds like bad design, if the two bases classes are so closely linked
      > shouldn't they be a single class. Or maybe you should make the dependent
      > class a member of the derived class.[/color]

      What I'm trying to do is extend a window encapsulation class. Base
      class A is the window and DDraw object (I didn't write it). Base class B is
      the DSound object (which needs the window handle created by Base class A).
      My derrived class extends the functionality of Base class A (blitting,
      sprites, etc) and adds sound from Base class B.
      I'll probably just make them members instead of inheriting.
      --

      Best wishes,
      Allen

      No SPAM in my email !!


      [color=blue]
      >
      > But if you write
      >
      > class D : public B1, public B2
      > {
      > D();
      > };
      >
      > Then it is guaranteed that B1 will be constructed before B2. So you can
      > write something like this
      >
      > D::D() : B2(get_ptr_from _b1())
      > {
      > }
      >
      > john
      >
      >[/color]


      Comment

      • Allen

        #4
        Re: base class initialization

        Oh yeah, Base class A doesn't init the window handle in the constructor.

        "Allen" <allen-terri-ng@att.SPAM.net > wrote in message
        news:nMyWa.8351 6$0v4.5576530@b gtnsc04-news.ops.worldn et.att.net...[color=blue]
        > Hi John,
        >
        > "John Harrison" <john_andronicu s@hotmail.com> wrote in message
        > news:bgd2tg$mtt ad$1@ID-196037.news.uni-berlin.de...[color=green]
        > >
        > > "Allen" <allen-terri-ng@att.SPAM.net > wrote in message
        > > news:B_nWa.8296 0$0v4.5532056@b gtnsc04-news.ops.worldn et.att.net...[color=darkred]
        > > > Hi all,
        > > >
        > > > I have a derived class that inherits from two base classes. One[/color][/color][/color]
        of[color=blue][color=green]
        > > the[color=darkred]
        > > > base class constructors takes as input a pointer that is initialized[/color][/color][/color]
        in[color=blue][color=green]
        > > the[color=darkred]
        > > > other base class. How can I make sure the base classes are properly
        > > > initialized with the data they need when I call the derived class'
        > > > constructor?
        > > > --
        > > >
        > > > Best wishes,
        > > > Allen
        > > >[/color]
        > >
        > > Sounds like bad design, if the two bases classes are so closely linked
        > > shouldn't they be a single class. Or maybe you should make the dependent
        > > class a member of the derived class.[/color]
        >
        > What I'm trying to do is extend a window encapsulation class. Base
        > class A is the window and DDraw object (I didn't write it). Base class B[/color]
        is[color=blue]
        > the DSound object (which needs the window handle created by Base class A).
        > My derrived class extends the functionality of Base class A (blitting,
        > sprites, etc) and adds sound from Base class B.
        > I'll probably just make them members instead of inheriting.
        > --
        >
        > Best wishes,
        > Allen
        >
        > No SPAM in my email !!
        >
        >
        >[color=green]
        > >
        > > But if you write
        > >
        > > class D : public B1, public B2
        > > {
        > > D();
        > > };
        > >
        > > Then it is guaranteed that B1 will be constructed before B2. So you can
        > > write something like this
        > >
        > > D::D() : B2(get_ptr_from _b1())
        > > {
        > > }
        > >
        > > john
        > >
        > >[/color]
        >
        >[/color]


        Comment

        • John Harrison

          #5
          Re: base class initialization


          "Allen" <allen-terri-ng@att.SPAM.net > wrote in message
          news:InzWa.8019 7$3o3.5513415@b gtnsc05-news.ops.worldn et.att.net...[color=blue]
          > Oh yeah, Base class A doesn't init the window handle in the constructor.
          >[/color]

          If I understand correctly you need a handle from DDraw for DSound, but DDraw
          does not create the handle for you in its constructor. Maybe this?

          class MyClass : public DDraw
          {
          public:
          MyClass()
          {
          // do whatever it takes to get a handle in DDraw
          Handle h = get_handle();
          sound = new DSound(handle);
          }
          private:
          DSound* sound;
          };

          Ugly.

          john


          Comment

          • Allen

            #6
            Re: base class initialization

            Hi John,

            Roughly, yes that is correct. The problem with that is that I want some
            of the second classes methods (DSound) exposed publicly through my derived
            class (which is why I was trying to inherit it in the first place--so I
            could just use "using" to expose the methods I wanted). Of course making it
            a private member doesn't allow that. So I guess what I'll have to do is
            declare member pointers to the methods I want access to.
            --

            Best wishes,
            Allen

            No SPAM in my email !!


            "John Harrison" <john_andronicu s@hotmail.com> wrote in message
            news:bgegse$njn v2$1@ID-196037.news.uni-berlin.de...[color=blue]
            >
            > "Allen" <allen-terri-ng@att.SPAM.net > wrote in message
            > news:InzWa.8019 7$3o3.5513415@b gtnsc05-news.ops.worldn et.att.net...[color=green]
            > > Oh yeah, Base class A doesn't init the window handle in the constructor.
            > >[/color]
            >
            > If I understand correctly you need a handle from DDraw for DSound, but[/color]
            DDraw[color=blue]
            > does not create the handle for you in its constructor. Maybe this?
            >
            > class MyClass : public DDraw
            > {
            > public:
            > MyClass()
            > {
            > // do whatever it takes to get a handle in DDraw
            > Handle h = get_handle();
            > sound = new DSound(handle);
            > }
            > private:
            > DSound* sound;
            > };
            >
            > Ugly.
            >
            > john
            >
            >[/color]


            Comment

            Working...