派生类中的运算符重新定义但仍使用父类

Operators in derived class redefining but still using parent class

本文关键字:定义 父类 运算符 新定义 派生      更新时间:2023-10-16

具体来说,我希望能够在基类的两个派生类中使用ostream operator <<

我正在创建的程序应该在"虚拟商店"中打印出各种"产品"的产品详细信息。产品中有两种不同类型的书籍。这些书中的每一本书都应该有自己的:

ID number
Author
NumberOfPages
Year

此外,ChildrensBook型需要保持最低年龄,TextBook需要保持等级。

我定义了类 Book 并从中派生出类 ChildrensBookTextBook 。我的问题是关于使用ostream operator <<打印出信息。

我可以在 Book 类中定义一个泛型<<函数,该函数将打印出两个派生类共有的所有信息,然后在派生类中重新定义<<时引用它?

例如

//The parent class
ostream& operator<<(ostream& bookOutput, const Book& printBook) {
    return bookOutput << printBook.ID << "Name " << printBook.name << "year:" << printBook.year";
}

然后在派生类中不知何故:

//The derived classes
ostream& operator<<(ostream& TextBookOutput, const TextBook& printTextBook) {
    return TextBookOutput << "TextBook: " 
           << "[Here is where I want to print out all the details of the book that are members of the base class]" << "Grade:" << printTextBook.grade;
}

所以我想我的问题可以总结为:我可以从子运算符中调用父运算符吗,如果是,我使用什么语法?

我想到的另一个想法是为使用父打印运算符的子级编写一个函数,然后从子级的打印运算符中调用该函数。 这意味着我在重新定义运算符时没有尝试调用运算符,但仍然要求使用父运算符并单独重新定义子运算符。

当然。

您有一个用于 Book s 的运算符,因此请使用它。您可以通过为它提供对一本书的引用来调用它,并且您可以使用多态性的力量来获取对基础的引用。

ostream& operator<<(ostream& TextBookOutput, const TextBook& printTextBook) {
    return TextBookOutput << "TextBook: " << static_cast<const Book&>(printTextBook) << "Grade:" << printTextBook.grade;
}
return TextBookOutput << static_cast<Book const &>(printTextBook) << ...

正如其他人指出的那样,您应该使用向下投射来实现您的要求。但我认为你也应该考虑一种不同的方法:你现在正在做的是混合静态动态多态性,这通常不是一个好主意(通常只会在以后表现出来)。

这是问题所在,请考虑您已经拥有的内容:

class Book { ... };
class TextBook : public Book { ... };
ostream& operator<<(ostream& os, const Book& book) {
    return os << "Book: " << book.name << "n";
}
ostream& operator<<(ostream& os, const TextBook& book) {
    return os << "TextBook: " << book.name << "n";
}
如果您像这样使用它,一切都会按预期进行:
Book book;
TextBook textBook;
cout << book << "n";     // prints out: Book: XYZ
cout << textBook << "n"; // prints out: TextBook: XYZ 
这是因为编译器将在编译时(静态)正确确定书籍的类型。

现在考虑另一种情况:

Book * textBook = new TextBook();
cout << *textBook << "n";    // prints out: Book: XYZ !

这是因为编译器无法知道它是什么高级类型,它可以是Book,TextBook或ChildrensBook。这只能在运行时(动态)使用虚函数等确定。

因此,如果您考虑使用动态多态性,我更喜欢这种方法:

class Book {
  public:
  virtual ostream& print(ostream& os) const { return os << "Book: XYZ"; }
  // Don't forget virtual destructor.
  virtual ~Book() {}
};
class TextBook : public Book {
  public:
  virtual ostream& print(ostream& os) const
  {
    // Here, you can also call the "print" method of the parent class
    // like this: os << Book::print(os);
    // or just invent your own like this:
    return os << "TextBook: XYZ";
  }
};
ostream& operator<<(ostream& os, const Book& book) {
  // Will correctly decide during runtime whether
  // to use Book::print or TextBook::print.
  return book.print(os);
}