標題: C++標準編程:虛函數與內聯 [打印本頁] 作者: 51黑tt 時間: 2016-3-5 17:05 標題: C++標準編程:虛函數與內聯 我們曾經在討論C++的時候,經常會問到:“虛函數能被聲明為內聯嗎?”現在,我們幾乎聽不到這個問題了。現在聽到的是:“你不應該使print成為內聯的。聲明一個虛函數為內聯是錯誤的!”
這種說法的兩個主要的原因是(1)虛函數是在運行期決議而內聯是一個編譯期動作,所以,我們將虛函數聲明為內聯并得不到什么效果;(2)聲明一個虛函數為內聯導致了函數的多分拷貝,而且我們為一個不應該在任何時候內聯的函數白白花費了存儲空間。這樣做很沒腦子。
不過,事實并不是這樣。我們先來看看第一個:許多情況下,虛擬函數都被靜態地決議了——比如在派生類虛擬函數中調用基類的虛擬函數的時候。為什么這樣做呢?封裝。一個比較明顯的例子就是派生類析構函數調用鏈。所有的虛析構函數,除了最初觸發這個析構鏈的虛析構函數,都被靜態的決議了。如果不將基類的虛析構函數內聯,我們無法從中獲利[a]。這和不內聯一個虛擬析構函數有什么不同嗎?如果繼承體系層次比較深并且有許多這樣的類的實例要被銷毀的話,答案是肯定的。
再來看另外一個不用析構函數的例子,想象一下設計一個圖書館類。我們將MaterialLocation作為抽象類LibraryMaterial的一個成員。將它的print成員函數聲明為一個純虛函數,并且提供函數定義:它輸出MaterialLocation。
class LibraryMaterial {
private:
MaterialLocation _loc; // shared data
// …
public:
// declares pure virtual function
inline virtual void print( ostream& = cout ) = 0;
};
// we actually want to encapsulate the handling of the
// location of the material within a base class
// LibraryMaterial print() method - we just don’t want it
// invoked through the virtual interface. That is, it is
// only to be invoked within a derived class print() method
inline void
LibraryMaterial::
print( ostream &os ) { os 《 _loc; }
接著,我們引入一個Book類,它的print函數輸出Title, Author等等。在這之前,它調用基類的print函數(LibraryMaterial::print())來顯示書本位置(MaterialLocation)。如下:
inline void
Book::
print( ostream &os )
{
// ok, this is resolved statically,
// and therefore is inline expanded …
LibraryMaterial::print();
os 《 "title:" 《 _title
《 "author" 《 _author 《 endl;
}