base class initialization

Discussion in 'C++' started by Allen, Aug 1, 2003.

  1. Allen

    Allen Guest

    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 !!
    Allen, Aug 1, 2003
    #1
    1. Advertising

  2. "Allen" <> wrote in message
    news:B_nWa.82960$...
    > 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
    >


    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
    John Harrison, Aug 1, 2003
    #2
    1. Advertising

  3. Allen

    Allen Guest

    Hi John,

    "John Harrison" <> wrote in message
    news:bgd2tg$mttad$-berlin.de...
    >
    > "Allen" <> wrote in message
    > news:B_nWa.82960$...
    > > 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
    > >

    >
    > 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.


    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 !!



    >
    > 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
    >
    >
    Allen, Aug 1, 2003
    #3
  4. Allen

    Allen Guest

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

    "Allen" <> wrote in message
    news:nMyWa.83516$...
    > Hi John,
    >
    > "John Harrison" <> wrote in message
    > news:bgd2tg$mttad$-berlin.de...
    > >
    > > "Allen" <> wrote in message
    > > news:B_nWa.82960$...
    > > > 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
    > > >

    > >
    > > 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.

    >
    > 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 !!
    >
    >
    >
    > >
    > > 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
    > >
    > >

    >
    >
    Allen, Aug 1, 2003
    #4
  5. "Allen" <> wrote in message
    news:InzWa.80197$...
    > Oh yeah, Base class A doesn't init the window handle in the constructor.
    >


    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
    John Harrison, Aug 1, 2003
    #5
  6. Allen

    Allen Guest

    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" <> wrote in message
    news:bgegse$njnv2$-berlin.de...
    >
    > "Allen" <> wrote in message
    > news:InzWa.80197$...
    > > Oh yeah, Base class A doesn't init the window handle in the constructor.
    > >

    >
    > 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
    >
    >
    Allen, Aug 2, 2003
    #6
    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. Alf P. Steinbach
    Replies:
    6
    Views:
    533
    John Carson
    Sep 3, 2005
  2. Dennis Jones
    Replies:
    5
    Views:
    341
    Grizlyk
    Jan 13, 2007
  3. Replies:
    4
    Views:
    326
    Joe Greer
    Jul 25, 2008
  4. Hicham Mouline
    Replies:
    1
    Views:
    583
    Victor Bazarov
    Apr 20, 2009
  5. Karan Rajput
    Replies:
    2
    Views:
    131
    Abinoam Jr.
    Dec 22, 2010
Loading...

Share This Page