Constructor implementation with RAII and inheritance

172 Views Asked by At

I'm developing a hardware library for our company's devices, which among other things needs to access a camera and different kinds of illumination. The basic hardware interface is designed like

public ref class IHardware abstract {

protected:
    ICamera* camera;
    ILight^ light;
    ...

public:
    IHarware();
    ...
    virtual bool acquireImage() abstract;
    ...
};

Here, ICamera and ILight are interfaces for the corresponding hardware parts. The DLL that I need to develop contains the above interface as well as a specialization of IHardware for our standard devices, let it be called SHardware. Spezializations for that device exist for ICamera and ILight as well, i.e. SCamera and SLight. Since I'd like to use RAII, the constructor of SHardware looks like

SHardware::SHardware : IHardware() {
    ...
    camera = new SCamera();
    camera->Init();
    light = gcnew SLight();
    light->Init();
    ...
}

Now, say I'd like to use that SHardware class for further spezialization, e.g. for a class called NHardware. I have classes NCamera inheiriting from SCamera and NLight inheiriting from SLight. My question is how to implement the constructor of NHardware; if it was like

NHardware::NHardware() : SHardware() {...}

camera and light are initialized in SHardware() already, so that I'd have to delete them before creating NCamera and NLight objects, which seems not correct to me. Of course I could extract camera and illumination initialization in a separate virtual function somehow and override it in the NHardware class, but when the SHardware constructor is called, isn't also the SHardware initialize function used first? I'm sure this is a quite common problem with rather simple solutions, but right now I just don't get it, so any help is appreciated! Thanks,

Matz

0

There are 0 best solutions below