首页 > c++ > 依赖于纯虚函数的重载函数的继承

依赖于纯虚函数的重载函数的继承 (Inheritance with Overloaded Functions That Rely on Pure Virtual Functions)

问题

我有一个基Write类,它有一个纯虚函数write (std::string text),需要所有派生类来实现它。在基类中,有一个重载write函数,它接受int并调用纯虚拟write()

在派生类中,我们write (std::string text)根据需要实现。

在主要的,我能够调用console.write("dog\n");,但我无法使用重载版本调用它,而不需要通过基类名称接收int Write。无论如何都要定义这个继承,以便两个write函数,一个接受a std::string和一个接受一个int而不通过遍历Write类名来提供继承细节的函数,如程序的最后一行所示?

write(int)' through the如果可能的话,我不希望用户能够调用重载的Write`类名。

#include <iostream>

class Write
{
protected:
    virtual void write (const std::string &text) = 0;

public:
    void write (const int &number)
    {
        write (std::to_string (number));
    }
};

class Console_Write : public Write
{
public:
    void write (const std::string &text) override
    {
        std::cout << text;
    }
};

int main()
{
    Console_Write console;
    console.write("dog\n");
    console.Write::write (1); // Is it possible to be able to change the inheritance so we can just call: console.write (1);
}

解决方法

正常的模式是让您的基类看起来像:

class Write {
 public:
  virtual ~Write() = default;
  void write(const std::string& str) {
    write_internal(str);
  }
  void write(int n) {
    write(std::to_string(n));
  }
 private:
  virtual void write_internal(const std::string& str) = 0;
}

class ConsoleWrite : public Write {
 public:
  ~ConsoleWrite() = default;
 private:
  void write_internal(const std::string& str) {
    std::cout << str;
  }
}

该模式甚至具有名称“非虚拟接口” - https://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Non-Virtual_Interface具有关于模式的更多信息。

问题

I have a base Write class that has a pure virtual function write (std::string text) that requires all derived classes to implement it. In the base class, there is an overloaded write function that takes in an int and calls the pure virtual write().

In the derived class, we implement the write (std::string text), as required.

In the main, I'm able to call console.write("dog\n");, but I'm not able to call it with the overloaded version that takes in an int without going through the base class name Write. Is there anyway to define this inheritance so that both write functions, one that takes in a std::string and one that takes in an int without giving away the details of the inheritance by going through the Write class name, as shown on the last line of the program?

I don't want the user to be able to call the overloaded write(int)' through theWrite` class name, if possible.

#include <iostream>

class Write
{
protected:
    virtual void write (const std::string &text) = 0;

public:
    void write (const int &number)
    {
        write (std::to_string (number));
    }
};

class Console_Write : public Write
{
public:
    void write (const std::string &text) override
    {
        std::cout << text;
    }
};

int main()
{
    Console_Write console;
    console.write("dog\n");
    console.Write::write (1); // Is it possible to be able to change the inheritance so we can just call: console.write (1);
}

解决方法

The normal pattern for this would be having your base class look something like:

class Write {
 public:
  virtual ~Write() = default;
  void write(const std::string& str) {
    write_internal(str);
  }
  void write(int n) {
    write(std::to_string(n));
  }
 private:
  virtual void write_internal(const std::string& str) = 0;
}

class ConsoleWrite : public Write {
 public:
  ~ConsoleWrite() = default;
 private:
  void write_internal(const std::string& str) {
    std::cout << str;
  }
}

The pattern even has a name "Non-Virtual Interface" - https://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Non-Virtual_Interface has more information about the pattern.

相似信息