views:

152

answers:

2

Hi,

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 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?

A: 

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

JRL
so you mean something like sizeButton : public Sizable {... private: TButton myButton;... }; ?? interesting...not sure how easily my event intercepting code will handle this...but I'll look into it, thanks!
hgh
+2  A: 

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();
    ...
};
Remy Lebeau - TeamB
If it's a pure virtual class with no implementation of its own, then it doesn't really suit my purposes - it's the implementation, not the interface which I'm trying to re-use. Thanks for the expl of why multi inheritance is not supported though!
hgh