如何轻松替换基类

发布于 2024-08-31 14:49:52 字数 1032 浏览 6 评论 0原文

我有以下类层次结构

class classOne
{
    virtual void abstractMethod() = 0;
};

class classTwo : public classOne
{
};

class classThree : public classTwo
{
};  

所有 classOne、classTwo 和 classThree 都是抽象类,我有另一个类定义纯虚方法

class classNonAbstract : public classThree
{
    void abstractMethod();

    // Couple of new methods
    void doIt();
    void doItToo();
};

现在我需要它不同......我需要它就像

class classNonAbstractOne : public classOne
{
    void abstractMethod();

    // Couple of new methods
    void doIt();
    void doItToo();
};

class classNonAbstractTwo : public classTwo
{
    void abstractMethod();

    // Couple of new methods
    void doIt();
    void doItToo();
};

and

class classNonAbstractThree : public classThree
{
    void abstractMethod();

    // Couple of new methods
    void doIt();
    void doItToo();
};

但所有非抽象类都有相同的新方法,具有相同的代码......我想避免将所有方法及其代码复制到每个非抽象类。我怎样才能做到这一点?

希望它是可以理解的...

I have the following hierarchy of classes

class classOne
{
    virtual void abstractMethod() = 0;
};

class classTwo : public classOne
{
};

class classThree : public classTwo
{
};  

All classOne, classTwo and classThree are abstract classes, and I have another class that is defining the pure virtual methods

class classNonAbstract : public classThree
{
    void abstractMethod();

    // Couple of new methods
    void doIt();
    void doItToo();
};

And right now I need it differently...I need it like

class classNonAbstractOne : public classOne
{
    void abstractMethod();

    // Couple of new methods
    void doIt();
    void doItToo();
};

class classNonAbstractTwo : public classTwo
{
    void abstractMethod();

    // Couple of new methods
    void doIt();
    void doItToo();
};

and

class classNonAbstractThree : public classThree
{
    void abstractMethod();

    // Couple of new methods
    void doIt();
    void doItToo();
};

But all the nonAbstract classes have the same new methods, with the same code...and I would like to avoid copying all the methods and it's code to every nonAbstract class. How could I accomplish that?

Hopefully it's understandable...

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

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

发布评论

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

评论(2

你的呼吸 2024-09-07 14:49:52
template<class Base>
struct Concrete : Base {
  void abstractMethod();

  void doIt() {
    // example of accessing inherited members:
    int n = Base::data_member; // or this->data_member
    n = Base::method(); // non-virtual dispatch
    n = this->method(); // virtual dispatch

    // since Base is a template parameter, 'data_member' and 'method' are
    // dependent names and using them unqualified will not properly find
    // them
  }
  void doItToo();
};

typedef Concrete<classOne> classNonAbstractOne; // if desired, for convenience

确保为您的抽象基类提供虚拟公共析构函数或使析构函数受保护(这样它不必是虚拟的,但仍然可以是)。

因为必须使用查找的名称来解析模板,但还不知道 Base 是什么,因此您需要使用 Base::memberthis->; member 访问继承的成员。

template<class Base>
struct Concrete : Base {
  void abstractMethod();

  void doIt() {
    // example of accessing inherited members:
    int n = Base::data_member; // or this->data_member
    n = Base::method(); // non-virtual dispatch
    n = this->method(); // virtual dispatch

    // since Base is a template parameter, 'data_member' and 'method' are
    // dependent names and using them unqualified will not properly find
    // them
  }
  void doItToo();
};

typedef Concrete<classOne> classNonAbstractOne; // if desired, for convenience

Make sure to give your abstract base classes either a virtual public destructor or make the destructor protected (then it doesn't have to be virtual, but still can be).

Because the template must be parsed with names looked up without yet knowing exactly what Base will be, you need to either use Base::member or this->member to access inherited members.

甜尕妞 2024-09-07 14:49:52

我通常会尽可能避免继承(除了定义纯接口的纯抽象类),因为它会产生紧密耦合。在许多情况下,组合是更好的选择。

此外,复杂的继承结构往往会让事情变得混乱。从您的描述中很难说出在这种特殊情况下什么是最好的。只是指出这一点作为经验法则。

I usually try to avoid inheritance if possible (except for pure abstract classes which define pure interfaces) because it creates a tight coupling. In many cases composition is the better alternative.

Also, things tend to get messy with complex inheritance structures. It's not easy to say from your description what's the best in this particular case. Just pointing this out as a rule of thumb.

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