c++ - 在C++中是否可以有虚拟类型?

标签 c++ oop inheritance module

我有一个MyClass类(带有几个虚函数),该类对名为MyType的对象执行操作。

MyClassImpl继承了MyClass并实现了虚函数,但是我需要在MyType中添加其他成员,但是我不想修改MyType类(相反,我想使其保持通用)。

现在,如果我创建一个MyTypeImpl并继承MyType,则可以添加成员。但是,如何使MyClassImpl(继承自MyClass)中的非虚函数使用新的MyTypeImpl

我能想到的唯一方法是使MyClass使用MyTypeImpl,但我想避免在通用类中使用该实现,因为我可能会使用各种不同的实现。

这是有关类外观的简单示例。当然,代码不会编译,因为方法和成员是在MyTypeImpl中添加的,而不是在MyType中添加的。

class MyType {
  public:
    void increment() {
      data_++;
    }
  protected:
    int data_ = 0;
};

class MyClass {
  public:   
    void alg() {
      sub_routine_1();
      sub_routine_2();
      modify_mytype();
    };

  protected:
    MyType mytype_;

    virtual void sub_routine_1() = 0;
    virtual void sub_routine_2() = 0;

    void modify_mytype() {
      mytype_.increment();
    };
};

class MyTypeImpl : public MyType {
  public:
    void decrement() {
      data_--;
      is_decremented = true;
    };

  protected:
    bool is_decremented = false;;
};

class MyClassImpl : public MyClass{
  public:
    void print() {
      mytype_.print();
    };
  protected:
    virtual void sub_routine_1() {
      //do algorithm things here
      mytype_.increment();
      mytype_.increment();
    };
    virtual void sub_routine_2() {
      //do more algorithm things here
      mytype_.decrement();
      mytype_.decrement();
    };
};

最佳答案

看完您的示例后,我现在看到您只想扩展该类的功能而无需修改原始类。如果您需要添加其他功能,但又不想更改存储在MyClass中的类型,那么我至少不修改MyType使其包含想要的功能的虚函数,这是我所不知道的打电话。
您还需要使MyClass带有指向MyType的指针,以便可以使用多态性并使调用解析为正确的实现:
动态多态解决方案:

#include <iostream>

class MyType {
  public:
    virtual void increment() {
      data_++;
    }

    // To be implemented by implementation class
    virtual void print() = 0;

    // To be implemented by implementation class
    virtual void decrement() = 0;

  protected:
    int data_ = 0;
};

class MyTypeImpl : public MyType
{
public:
    void print() {
        std::cout << 42 << std::endl;
    }

    void decrement() {
        data_--;
        is_decremented = true;
    };

protected:
    bool is_decremented = false;;
};

class MyClass {
  public:
    MyClass(MyType* mytype)
        : mytype_(mytype)
    {}

    void alg() {
      sub_routine_1();
      sub_routine_2();
      modify_mytype();
    };

  protected:
    MyType* mytype_;

    virtual void sub_routine_1() = 0;
    virtual void sub_routine_2() = 0;

    void modify_mytype() {
      mytype_->increment();
    };
};

class MyClassImpl : public MyClass{
  public:
    MyClassImpl(MyType* mytype)
        : MyClass(mytype)
    {}

    void print() {
      mytype_->print();
    };
  protected:
    virtual void sub_routine_1() {
      //do algorithm things here
      mytype_->increment();
      mytype_->increment();
    };
    virtual void sub_routine_2() {
      //do more algorithm things here
      mytype_->decrement();
      mytype_->decrement();
    };
};

int main()
{
    MyType* mytype = new MyTypeImpl();
    MyClass* myclass = new MyClassImpl(mytype);

    // Prints "42"
    myclass->print();

    // Do other stuff with "myclass"

    delete myclass;
    delete mytype;
}

Note, I am only using a raw pointer in this example for increased clarity. It is highly recommended that you don't use new and delete and use smart pointers to manage the lifetime of your pointers instead.


静态多态解决方案:
并不是说该解决方案的设计实际上会更好,但是我认为这更接近您的实际需求,因为它不需要直接修改MyType类。 MyClass唯一需要的修改就是使其成为模板类:
#include <iostream>

class MyType {
  public:
    virtual void increment() {
      data_++;
    }

  protected:
    int data_ = 0;
};

class MyTypeImpl : public MyType
{
public:
    void print() {
        std::cout << data_ << std::endl;
    }

    void decrement() {
        data_--;
        is_decremented = true;
    };

protected:
    bool is_decremented = false;
};

template <typename T>
class MyClass {
  public:

    void alg() {
      sub_routine_1();
      sub_routine_2();
      modify_mytype();
    };

  protected:
    T mytype_;

    virtual void sub_routine_1() = 0;
    virtual void sub_routine_2() = 0;

    void modify_mytype() {
      mytype_.increment();
    };
};

template <typename T>
class MyClassImpl : public MyClass<T> {
  public:
    void print() {
      this->mytype_.print();
    };

  protected:
    virtual void sub_routine_1() {
      //do algorithm things here
        this->mytype_.increment();
        this->mytype_.increment();
    };
    virtual void sub_routine_2() {
      //do more algorithm things here
        this->mytype_.decrement();
        this->mytype_.decrement();
    };
};

int main()
{
    // Use the template to get the correct implementation
    MyClassImpl<MyTypeImpl> myclass;

    myclass.alg();
    myclass.print();

    // Do other stuff with my class
}

关于c++ - 在C++中是否可以有虚拟类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59742156/

相关文章:

python - 需要在 python 测试用例中模拟一些基类行为

c++ - 在 C++ 中声明接口(interface)并使用多重继承实现接口(interface)

c++ - 如何轻松编写克隆方法?

c++ - 球体的 OpenGL UV 贴图(有接缝)

c++ - Win32 消息循环中的键盘输入

php - 让javascript函数基于PHP变量返回状态的好方法?

c++ - 获取反转的 boost 图的拷贝

PHP 类不返回任何内容

PHP 面向对象 : do something when object is assigned as a property

C++:在编译与运行时禁止派生类中的虚函数