I have several classes derived from a top-level superclass:
base: CDeviceClientRequest sub-class: CDeviceSetEnabledRequest (base) inst-class: CDeviceGPSSetEnabledRequest* (sub-class) inst-class: CDeviceTotalStationSetEnabledRequest* (sub-class)
Only those marked with a * are instantiable, the others have protected constructors, i.e. the constructors invoke the base class constructors, setting the private member variables.
I am constructing them via a method:
CDeviceClientRequest getRequest(int type)
{
switch (type)
{
GPS_SET_ENABLED: return CDeviceGPSSetEnabledRequest();
TS_SET_ENABLED : return CDeviceTotalStationSetEnabledRequest();
default:
// raise an unknown type exception
}
}
I then have some code that invokes the request:
void invoke(CDeviceClientRequest& request)
{
// some code
}
So I have code somewhere that looks like this:
CDeviceClientRequest request = getRequest(GPS_SET_ENABLED);
invoke(request);
My problem is that although I am invoking the constructor for CDeviceGPSSetEnabledRequest(), what gets returned from getRequest(), is a CDeviceClientRequest, and what is passed to invoke is also a CDeviceClientRequest, and not a CDeviceGPSSetEnabledRequest, like I expected.
I verified this by adding a simple whatAmI() method to all super and base classes to std::cout the name of the class, and I only ever get "I am a CDeviceClientRequest". Somewhere the fact that it is dealing with a derived class has been lost.
Any help greatly appreciated. (Note: I have greatly simplified the code for the post).