Archived

This topic is now archived and is closed to further replies.

Generic Programming and Inheritance..

This topic is 5177 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 guys, I got a question for ya. For example, I want to implement an interface for input devices. So, I go:
class IDeviceState
{
public:
	
	IDeviceState() {};
	~IDeviceState() {};

private:
	long m_dwFlags;
};


class IInputDevice
{
public:

	// Basically a state for a keyboard is the info about pressed keys

	// and for mouse it''s the position/button

        IDeviceState *GetState()
	{ return m_pDeviceState; };

	virtual void ResetState() = 0;
	
	IInputDevice()
	{
		m_pDeviceState = NULL;
		m_eDeviceType = INPUTDEV_NONE;
	};
	virtual ~IInputDevice() {};

public:
	eInputDeviceType m_eDeviceType;
	IDeviceState *m_pDeviceState;
};
OK, this is a generic interface for all other devices that are going to inherit it, ok? So, I want to add a mouse input device:
#include "InputDevice.h"

struct sMouseDeviceState : public IDeviceState
{
	bool *m_pButtonDown;
	int m_iNumButtons;
	
	sMouseDeviceState()
	{
		m_pButtonDown = NULL;
		m_iNumButtons = 0;
	};

	~sMouseDeviceState()
	{
		if(m_pButtonDown)
			delete m_pButtonDown;
	};
};

class CMouseInputDevice : public IInputDevice
{
public:

	void ResetState();

	CMouseInputDevice();
	virtual ~CMouseInputDevice();

};
Also I want to have a keyboard input device:
#include "InputDevice.h"

struct sKeyboardDeviceState : public IDeviceState
{
	char *m_cKeys;
	int m_iNumKeys;
	// Add NumLock, CapsLock or whatever here too

};

class CKeyboardInputDevice : public IInputDevice
{
public:

	void ResetState();

	CKeyboardInputDevice();
	virtual ~CKeyboardInputDevice();

};
Ok, we got so far. Now I want to implement an Input Engine that holds all input devices and acts on them:
#include "KeyboardInputDevice.h"
#include "MouseInputDevice.h"

class CInputEngine
{

public:

	bool Create();
	void Destroy();

	IInputDevice *GetDevice(eInputDeviceType type);

	CInputEngine();
	virtual ~CInputEngine();

public:
	vector< IInputDevice *> m_DeviceList;

public:
	IInputDevice *QueryDevice(eInputDeviceType type);
};
So, the question is: is there any better way to do that kind of inheritance so it will still be pretty generic and easy to use? This example works only with lots of type castings which is not really good. Please help. Thanks. " Do we need us? "

Ionware Productions - Games and Game Tools Development

Share this post


Link to post
Share on other sites
I don't know enough about keyboards, mice, or joysticks to comment on what the specific interface should be, but I can point you to the C++ FAQs about proper inheritance:

http://www.parashift.com/c++-faq-lite/proper-inheritance.html

--
Dave Mikesell Software & Consulting

[edited by - dmikesell on October 15, 2003 8:08:04 AM]

Share this post


Link to post
Share on other sites
What are you trying to gain from this kind of inheritance? There are so few types of input device and so little similarity between them that I can''t see any significant benefit from using a common base class.

[ MSVC Fixes | STL Docs | SDL | Game AI | Sockets | C++ Faq Lite | Boost
Asking Questions | Organising code files | My stuff | Tiny XML | STLPort]

Share this post


Link to post
Share on other sites
You are definitely over-using inheritance by doing this. There is virtually nothing common about a keyboard and mouse, and there''s no reason why they should be placed in an inheritance hierarchy.

-Sirp.

Battle For Wesnoth: A Free fantasy turn-based strategy game - http://www.wesnoth.org

Share this post


Link to post
Share on other sites
I know that there is very little similarity between input devices, but I just want them to use the same pattern when retrieving/adding/removing them from input device.

Well, thanks anyway to all that has answered this thread, I''ll consider some of the words said here.

" Do we need us? "


Ionware Productions - Games and Game Tools Development

Share this post


Link to post
Share on other sites
quote:
Original post by HellRiZZer
I know that there is very little similarity between input devices, but I just want them to use the same pattern when retrieving/adding/removing them from input device.


Just name the member functions the same thing.

You are making things far more difficult by forcing inheritance in this case. How do you know what kind of struct GetDevice() is going to return? Oops, you have to cast it.

The fact that you have to cast should be a red flag that you should use similar semantics (name member vars the same thing) but NOT inheritance. Base classes should serve some purpose besides "I want them to have the same member function name."

I''d rethink the InputEngine as well.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
use a common base class and let it provide a (probable) ammount no of digital and analog controllers. signal these back to the application through a single callback.
that way it''ll be alot easier to add new input devices and configure the controls.

Share this post


Link to post
Share on other sites