Hello,
I'm trying to write a base class and then several derived classes to work with some usb devices from 'Phidget' (
http://www.phidgets.com/)
I want the base class to provide functions that are common to all the different type of Phidget modules e.g. temperature sensors, motor controllers. These are functions like 'getDeviceSerialNumber' etc. I've made the base class abstract by including a couple of pure virtual functions, one in particular is called createHandle(ptr). The intention of this is so that each derived class must implement its own createHandle function as the Phidget API has seperate handle creation functions for each different module e.g. createAccelerometerHandle(),createStepperMotorHandle() etc.
So far this is the (relevant parts) of the base class :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
class Interface_PhidgetModule {
public:
.. Irrelevant code removed ..
/*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*
* ABSTRACT public member functions
*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*/
virtual int createHandle()=0; //<==== this is causing problems
virtual int onDetachHandler() = 0;
private:
virtual int open(CPhidgetHandle hnd, int serialNumber) = 0;
protected:
};
|
Now the problem comes when I try to derive a class, more specifically when I try to override the createHandle() method :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
class PhidgetAccelerometer : public Interface_PhidgetModule{
public:
/*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*
* Virtual functions declared
* in the base class
*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*/
virtual int createHandle(CPhidgetHandle accel); //<==== Different to base
virtual int onDetachHandler();
private:
//static map<int,CPhidgetAccelerometerHandle> accelerometers;
std::map<CPhidgetAccelerometerHandle,CPhidgetAccelerometerHandle*> accelerometers;
virtual int open(CPhidgetHandle hnd, int serialNumber);
};
|
As you can see, I've prototyped the createHandle function in the DERIVED class with a parameter, but as I've just found out, the compiler doesn't like this ! If I understand correctly now, if I want to pass a parameter in the derived class' createHandle() function, I have to declare it in the base class pure virtual function prototype. The problem is this though :
1) All derived classes MUST have the createHandle() function AND
2) Each derived createHandle() function must accept a different type of handle
How can I make a pure virtual function in the base class accept different types of input parameters ? I've considered using templates and such but I just can't figure it out.