By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,898 Members | 2,039 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,898 IT Pros & Developers. It's quick & easy.

base class initialization

P: n/a
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 !!


Jul 19 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a

"Allen" <al************@att.SPAM.net> wrote in message
news:B_*********************@bgtnsc04-news.ops.worldnet.att.net...
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
Jul 19 '05 #2

P: n/a
Hi John,

"John Harrison" <jo*************@hotmail.com> wrote in message
news:bg************@ID-196037.news.uni-berlin.de...

"Allen" <al************@att.SPAM.net> wrote in message
news:B_*********************@bgtnsc04-news.ops.worldnet.att.net...
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

Jul 19 '05 #3

P: n/a
Oh yeah, Base class A doesn't init the window handle in the constructor.

"Allen" <al************@att.SPAM.net> wrote in message
news:nM*********************@bgtnsc04-news.ops.worldnet.att.net...
Hi John,

"John Harrison" <jo*************@hotmail.com> wrote in message
news:bg************@ID-196037.news.uni-berlin.de...

"Allen" <al************@att.SPAM.net> wrote in message
news:B_*********************@bgtnsc04-news.ops.worldnet.att.net...
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


Jul 19 '05 #4

P: n/a

"Allen" <al************@att.SPAM.net> wrote in message
news:In*********************@bgtnsc05-news.ops.worldnet.att.net...
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
Jul 19 '05 #5

P: n/a
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" <jo*************@hotmail.com> wrote in message
news:bg************@ID-196037.news.uni-berlin.de...

"Allen" <al************@att.SPAM.net> wrote in message
news:In*********************@bgtnsc05-news.ops.worldnet.att.net...
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

Jul 19 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.