C++标准编程:虚函数与内联
来源:优易学  2010-1-15 9:26:51   【优易学:中国教育考试门户网】   资料下载   IT书店

 

  LibraryMaterial::print();

  os << "title:" << _title

  << "author" << _author << endl;

  }

  AudioBook类,派生于Book类,并加入附加信息,比如旁述,音频格式等等。这些东西都用它的print函数输出。再这之前,我们需要调用Book::print()来显示前面的信息。

  inline void

  AudioBook::

  print( ostream &os )

  {

  // ok, this is resolved statically,

  // and therefore is inline expanded ...

  Book::print();

  os << "narrator:" << _narrator << endl;

  }

  这和虚析构函数调用链的例子一样,都只是最初调用的虚函数没有被静态决议,其它的都被原地展开。This unnamed hierarchical design pattern is significantly less effective if we never declare a virtual function to be inline.

  那么对于第二个原因中代码膨胀的问题呢?我们来分析一下,如果我们写下:

  LibraryMaterial *p =

  new AudioBook( "Mason & Dixon",

  "Thomas Pynchon", "Johnny Depp" );

  // ...

  p->print();

上一页  [1] [2] [3] 下一页

责任编辑:cyth

文章搜索:
 相关文章
热点资讯
资讯快报
热门课程培训