VCL多重继承

发布于 2024-08-09 04:51:40 字数 636 浏览 7 评论 0原文

我正在尝试开发一组控件,它们在大小调整方面都有许多常见的行为。我认为这是一个需要多重继承的实例(尽管我完全愿意接受任何相反的建议)。我想做的基本上是混合模式

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

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

class sizeEdit : public Sizable, public TEdit {
...
};

等等...

我已经在 Sizing 类中编写了大量的大小调整代码并对其进行了测试,它很好,但现在我已经列出了结构的其余部分(是的,我可能应该先为类编写一个框架)并且遗憾地发现:

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

我已经取出了所有不需要是成员函数的函数(例如测量字符串的长度),但仍然有很多功能这是不可能的。

有没有人有任何设计建议,这样我就不必重复大量代码?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

︶ ̄淡然 2024-08-16 04:51:40

Delphi(以及主要用Delphi 编写的VCL)不支持类的多重继承。然而,从 v6 开始,当使用 VCL 类作为祖先时,它确实支持接口的多重继承,例如:

// 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();
    ...
}; 

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();
    ...
}; 
倚栏听风 2024-08-16 04:51:40

为什么不让 TButton 成为一个成员(组合)而不是继承它?

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文