Jump to content
  • Advertisement
Sign in to follow this  
Winegums

Inheriting from classes with no base constructor

This topic is 4154 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!