base class initialization

A

Allen

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

John Harrison

Allen said:
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?

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
 
A

Allen

Hi John,

John Harrison said:
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 !!
 
J

John Harrison

Allen said:
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
 
A

Allen

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top