Inheriting from classes with no base constructor

Started by
2 comments, last by Zahlman 17 years, 1 month ago
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]
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.
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){    ....}
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.

This topic is closed to new replies.

Advertisement