• 所谓virtual constructor是某种函数,视其取得的输出,可产生不同类型的对象。
  • 有一种特地的virtual constructor——所谓virtual copy constructor——也被宽泛地使用、virtual copy constructor会返回一个指针,指向其调用者(某对象)的一个新正本。基于这种行为,virtual copy constructor通常以copyself或cloneself命令,或者间接命名为clone。
  • 当derived class从新定义其base class的一个虚函数时,不再须要肯定得申明与本来雷同的返回类型。如果函数的返回类型是个指针或援用,指向一个base class,那么derived class的函数能够返回一个指向该base class的derived class的指针或援用。这样就能够申明出像virtual copy constructor这样的函数。
  • non-member function的虚化非常容易:写一个虚函数做理论工作,再写一个什么都不做的非虚函数,只负责调用虚函数。为了缩小函数调用老本,能够将非虚函数inline化。
  • 上面就是virtual copy constructor和virtual non-member function的例子。

    #include <iostream>#include <list>class NLComponent{public:  // 申明 virtual copy constructor  virtual NLComponent *clone() const = 0;  virtual std::ostream& print(std::ostream& s) const = 0;  NLComponent() {}  virtual ~NLComponent() {}};class TextBlock : public NLComponent{public:  // virtual copy constructor  TextBlock *clone() const override {return new TextBlock(*this);}  std::ostream& print(std::ostream& s) const override  {      s << "I'm TextBlock" << std::endl;      return s;  }};class Graphic : public NLComponent{public:  // virtual copy constructor  Graphic *clone() const override {return new Graphic(*this);}  std::ostream& print(std::ostream& s) const override  {      s << "I'm Graphic" << std::endl;      return s;  }};// 将 Non-Member Function的行为虚化inline std::ostream& operator<<(std::ostream& s, const NLComponent& c){  return c.print(s);}class NewsLetter{public:  NewsLetter() {}  ~NewsLetter() {}  // copy constructor  NewsLetter(const NewsLetter& rhs);  void AddComponet(NLComponent* c);  void PrintComponents();private:  std::list<NLComponent*> components;};NewsLetter::NewsLetter(const NewsLetter& rhs){  for (auto it = rhs.components.begin(); it != rhs.components.end(); it++)  {      components.push_back((*it)->clone());  }}void NewsLetter::AddComponet(NLComponent* c){  components.push_back(c);}void NewsLetter::PrintComponents(){  for (auto it = components.begin(); it != components.end(); it++)  {      std::cout << *(*it);  }}int main(){  TextBlock tb;  Graphic gp;  NewsLetter nl1;  nl1.AddComponet(&tb);  nl1.AddComponet(&gp);  NewsLetter nl2(nl1);  nl2.PrintComponents();}