开发者

VCL multiple inheritance

开发者 https://www.devze.com 2022-12-10 00:16 出处:网络
I\'m trying to develop a set of controls which all have a number of common behaviours with respect to sizing.I think that this is an instance where multiple inheritance is required (although am fully

I'm trying to develop a set of controls which all have a number of common behaviours with respect to sizing. I think that this is an instance where multiple inheritance is required (although am fully willing to accept any advice to the contrary). What I would like to do is basically a mixin pattern

class Sizable {        
    ...
    public:
        ResizeMe();
        ResetText();
        ...
};

class sizeButton : public Sizable, public TButton {
...
};

class开发者_如何学JAVA sizeEdit : public Sizable, public TEdit {
...
};

and so forth...

I have written a non-trivial amount of sizing code in the Sizable class and tested it and it's nice, but now I have set out the rest of the structure (yes, I probably should have written a skeleton for the classes first) and have discovered that sadly:

[BCC32 Error] szButton.h(15): E2278 Multiple base classes not supported for VCL classes

I have pulled out all the functions which aren't required to be member functions (e.g. measuring the length of strings), but there are still a lot of functions where this is not possible.

Does anyone have any design advice so that I don't have to duplicate a ton of code?


Delphi (and thus the VCL, which is mostly written in Delphi) does not support multiple inheritance of classes. However, in v6 onwards, it does support multiple inheritance of interfaces when a VCL class is used as an ancestor, for example:

// must be a pure virtual class with no data members
// and no implementation of its own...
class Sizable
{
public:
    virtual void ResizeMe() = 0;
    virtual void ResetText() = 0;
    ...
};

class sizeButton : public TButton, public Sizable
{
public:
    virtual void ResizeMe();
    virtual void ResetText();
    ...
}; 

class sizeEdit : public TEdit, public Sizable
{
public:
    virtual void ResizeMe();
    virtual void ResetText();
    ...
}; 


Why not make the TButton a member (composition) rather than inherit from it?

0

精彩评论

暂无评论...
验证码 换一张
取 消