inheritance

A

Allen

Hi all,

I'm still having a hard time with my derived class. I know I make
reference to some MS-specific tech. but the question should be independent
of that; I'm having a hard time with the inheritance/classes. Here is what
I've got, paracoding for brevity:

//encapsulates the display and the DirectDraw COM object
class baseclass_DDraw
{
public:
baseclass_DDraw();
~baseclass_DDraw();
InitWindow(); //this is where m_hWnd is init'd
private:
friend class derrivedclass_MyDisplay;
HWND m_hWnd;
};

//encapsulates the DirectSound COM object
class baseclass_DSound
{
public:
baseclass_DSound(HWND hWnd); //needs to be the value created by
//baseclass_DDraw--THIS IS THE
PROBLEM!
~baseclass_DSound();
};

//extends the functionality of baseclass_DDraw, includes sound support from
//baseclass_DSound, and adds input support locally
class derrivedclass_MyDisplay : baseclass_DDraw
{
public:
derrivedclass_MyDisplay();
~derrivedclass_MyDisplay();
MyInputFunc();
private:
baseclass_DSound* privatepntr;
};

derrivedclass_MyDisplay()
{
InitWindow(); //m_hWnd is now init'd
privatepntr = new baseclass_DSound(m_hWnd);
}

Whew! The problem is that now I can't seem to expose the methods I need
from baseclass_DSound because it is private. I tried creating pointers to
the baseclass_DSound methods that I wanted exposed and then making those
pointers public members but, that gave me lots of weird errors.

I would be very grateful to anyone willing/able to help.
--

Best wishes,
Allen

No SPAM in my email !!
 
J

John Harrison

Allen said:
Hi all,

I'm still having a hard time with my derived class. I know I make
reference to some MS-specific tech. but the question should be independent
of that; I'm having a hard time with the inheritance/classes. Here is what
I've got, paracoding for brevity:

//encapsulates the display and the DirectDraw COM object
class baseclass_DDraw
{
public:
baseclass_DDraw();
~baseclass_DDraw();
InitWindow(); //this is where m_hWnd is init'd
private:
friend class derrivedclass_MyDisplay;
HWND m_hWnd;
};

//encapsulates the DirectSound COM object
class baseclass_DSound
{
public:
baseclass_DSound(HWND hWnd); //needs to be the value created by
//baseclass_DDraw--THIS IS THE
PROBLEM!
~baseclass_DSound();
};

//extends the functionality of baseclass_DDraw, includes sound support from
//baseclass_DSound, and adds input support locally
class derrivedclass_MyDisplay : baseclass_DDraw
{
public:
derrivedclass_MyDisplay();
~derrivedclass_MyDisplay();
MyInputFunc();
private:
baseclass_DSound* privatepntr;
};

derrivedclass_MyDisplay()
{
InitWindow(); //m_hWnd is now init'd
privatepntr = new baseclass_DSound(m_hWnd);
}

Whew! The problem is that now I can't seem to expose the methods I need
from baseclass_DSound because it is private. I tried creating pointers to
the baseclass_DSound methods that I wanted exposed and then making those
pointers public members but, that gave me lots of weird errors.

I would be very grateful to anyone willing/able to help.

Its not obvious to me that you should be using inheritance at all. You
problably want delegation, its a common newbie mistake to use inheritance
where delegation is better. However can't be sure, only you really know what
you are doing.

Suppose play_beethovens_ninth is a method on the baseclass_DSound class that
you want exposed on the derrivedclass_MyDisplay object then you do it like
this

class derrivedclass_MyDisplay : public baseclass_DDraw
{
public:
derrivedclass_MyDisplay();
~derrivedclass_MyDisplay();
void play_beethovens_ninth()
{
privatepntr->play_beethovens_ninth();
}
private:
baseclass_DSound* privatepntr;
};

This is called delegation, you define play_beethovens_ninth in the
derrivedclass_MyDisplay class but all it does is forward the call to the
play_beethovens_ninth method on the baseclass_DSound class using the
baseclass_DSound pointer.

john
 
A

Allen

Hi John,

Thanks for the input. I was kinda trying to avoid that--having two
function calls for one function--but that's starting to look like the best
option.
--

Best wishes,
Allen

No SPAM in my email !!
 
K

Kevin Goodsell

Allen said:
//encapsulates the display and the DirectDraw COM object
class baseclass_DDraw
{
public:
baseclass_DDraw();
~baseclass_DDraw();

The name suggests you intend to use this as a base class. If so, you
need to make your destructor virtual. Base class destructors should
almost always be virtual.

-Kevin
 

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
474,138
Messages
2,570,801
Members
47,348
Latest member
nethues

Latest Threads

Top