Sign in to follow this  
Winegums

Inheriting from classes with no base constructor

Recommended Posts

Hi, so I'm trying to create a class than inherits from two base classes. One is theSphere, which encapsulates material and mesh code for a D3DXSPHERE. The second class is theSound, which encapsulates code for a DirectAudio sound buffer. I'm trying to inherit them to a theSoundObject class, which gives a visual representation of the sound source on screen (with a sphere ya see...). The constructor for theSphere takes no parameters, while theSound takes (LPDIRECTSOUNDBUFFER8 buff, DWORD flags). how do i create a class that inherits from both of these? from what i understand classes that inherit from base classes call the base class constructors when they're created? I tried class theSoundObject : public theSphere, public theSound(LPDIRECTSOUNDBUFFER8 buff, DWORD flags) but I get an error ( missing ',' before '(' ) . [Edited by - Winegums on March 4, 2007 9:40:57 PM]

Share this post


Link to post
Share on other sites
Initialize the base class constructor through the derived classes constructor:

class theSoundObject : public theSphere, public theSound {

public:
theSoundObject ()
: theSound (LPDIRECTSOUNDBUFFER8 buff, DWORD flags) {

}
};

Of course, you will have to pass the apropariate parameters.

Share this post


Link to post
Share on other sites
You're on the right track, but you want the section in parenthesis on your constructor, not on the class decleration.
Your class should probably looks something like this:

class theSoundObject : public theSphere, public theSound
{
public:
theSoundObject(LPDIRECTSOUNDBUFFER8 buff, DWORD flags);
....
}

....

theSoundObject::theSoundObject(LPDIRECTSOUNDBUFFER8 buff, DWORD flags) : theSphere(), theSound(buff, flags)
{
....
}


BTW, there's a lot of reasons why you shouldn't use multiple inheritance. I'm not going to go into it, but in-case anyone rants at you, you could also design this using aggregation instead of inheritance:

class theSoundObject
{
public:
theSoundObject(LPDIRECTSOUNDBUFFER8 buff, DWORD flags);
....
private:
theSphere mySphere;
theSound mySound;
}

....

theSoundObject::theSoundObject(LPDIRECTSOUNDBUFFER8 buff, DWORD flags) : mySphere(), mySound(buff, flags)
{
....
}

Share this post


Link to post
Share on other sites
Quote:
Original post by Hodgman
BTW, there are a lot of reasons why you should be careful with multiple inheritance and consider using aggregation instead first before you consider MI.


To be more pragmatic about it.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this