解析C++編程中virtual聲明的虛函數(shù)以及單個繼承
虛函數(shù)
虛函數(shù)是應(yīng)在派生類中重新定義的成員函數(shù)。 當(dāng)使用指針或?qū)惖囊脕硪门缮念悓ο髸r,可以為該對象調(diào)用虛函數(shù)并執(zhí)行該函數(shù)的派生類版本。
虛函數(shù)確保為該對象調(diào)用正確的函數(shù),這與用于進行函數(shù)調(diào)用的表達式無關(guān)。
假定基類包含聲明為 virtual 的函數(shù),并且派生類定義了相同的函數(shù)。 為派生類的對象調(diào)用派生類中的函數(shù),即使它是使用指針或?qū)惖囊脕碚{(diào)用的。 以下示例顯示了一個基類,它提供了 PrintBalance 函數(shù)和兩個派生類的實現(xiàn)
// deriv_VirtualFunctions.cpp // compile with: /EHsc #include <iostream> using namespace std; class Account { public: Account( double d ) { _balance = d; } virtual double GetBalance() { return _balance; } virtual void PrintBalance() { cerr << "Error. Balance not available for base type." << endl; } private: double _balance; }; class CheckingAccount : public Account { public: CheckingAccount(double d) : Account(d) {} void PrintBalance() { cout << "Checking account balance: " << GetBalance() << endl; } }; class SavingsAccount : public Account { public: SavingsAccount(double d) : Account(d) {} void PrintBalance() { cout << "Savings account balance: " << GetBalance(); } }; int main() { // Create objects of type CheckingAccount and SavingsAccount. CheckingAccount *pChecking = new CheckingAccount( 100.00 ) ; SavingsAccount *pSavings = new SavingsAccount( 1000.00 ); // Call PrintBalance using a pointer to Account. Account *pAccount = pChecking; pAccount->PrintBalance(); // Call PrintBalance using a pointer to Account. pAccount = pSavings; pAccount->PrintBalance(); }
在前面的代碼中,對 PrintBalance 的調(diào)用是相同的,pAccount 所指向的對象除外。 由于 PrintBalance 是虛擬的,因此將調(diào)用為每個對象定義的函數(shù)版本。 派生類 PrintBalance 和 CheckingAccount 中的 SavingsAccount 函數(shù)“重寫”基類 Account 中的函數(shù)。
如果聲明的類不提供 PrintBalance 函數(shù)的重寫實現(xiàn),則使用基類 Account 中的默認實現(xiàn)。
派生類中的函數(shù)僅在基類中的虛函數(shù)的類型相同時重寫這些虛函數(shù)。 派生類中的函數(shù)不能只是與其返回類型中的基類的虛函數(shù)不同;參數(shù)列表也必須不同。
當(dāng)使用指針或引用調(diào)用函數(shù)時,以下規(guī)則將適用:
根據(jù)為其調(diào)用的對象的基本類型來解析對虛函數(shù)的調(diào)用。
根據(jù)指針或引用的類型來解析對非虛函數(shù)的調(diào)用。
以下示例說明在通過指針調(diào)用時虛函數(shù)和非虛函數(shù)的行為:
// deriv_VirtualFunctions2.cpp // compile with: /EHsc #include <iostream> using namespace std; class Base { public: virtual void NameOf(); // Virtual function. void InvokingClass(); // Nonvirtual function. }; // Implement the two functions. void Base::NameOf() { cout << "Base::NameOf\n"; } void Base::InvokingClass() { cout << "Invoked by Base\n"; } class Derived : public Base { public: void NameOf(); // Virtual function. void InvokingClass(); // Nonvirtual function. }; // Implement the two functions. void Derived::NameOf() { cout << "Derived::NameOf\n"; } void Derived::InvokingClass() { cout << "Invoked by Derived\n"; } int main() { // Declare an object of type Derived. Derived aDerived; // Declare two pointers, one of type Derived * and the other // of type Base *, and initialize them to point to aDerived. Derived *pDerived = &aDerived; Base *pBase = &aDerived; // Call the functions. pBase->NameOf(); // Call virtual function. pBase->InvokingClass(); // Call nonvirtual function. pDerived->NameOf(); // Call virtual function. pDerived->InvokingClass(); // Call nonvirtual function. }
輸出
Derived::NameOf Invoked by Base Derived::NameOf Invoked by Derived
請注意,無論 NameOf 函數(shù)是通過指向 Base 的指針還是通過指向 Derived 的指針進行調(diào)用,它都會調(diào)用 Derived 的函數(shù)。 它調(diào)用 Derived 的函數(shù),因為 NameOf 是虛函數(shù),并且 pBase 和 pDerived 都指向類型 Derived 的對象。
由于僅為類類型的對象調(diào)用虛函數(shù),因此不能將全局函數(shù)或靜態(tài)函數(shù)聲明為 virtual。
在派生類中聲明重寫函數(shù)時可使用 virtual 關(guān)鍵字,但它不是必需的;虛函數(shù)的重寫始終是虛擬的。
必須定義基類中的虛函數(shù),除非使用 pure-specifier 聲明它們。 (有關(guān)純虛函數(shù)的詳細信息,請參閱抽象類。)
可通過使用范圍解析運算符 (::) 顯式限定函數(shù)名稱來禁用虛函數(shù)調(diào)用機制。 考慮先前涉及 Account 類的示例。 若要調(diào)用基類中的 PrintBalance,請使用如下所示的代碼:
CheckingAccount *pChecking = new CheckingAccount( 100.00 ); pChecking->Account::PrintBalance(); // Explicit qualification. Account *pAccount = pChecking; // Call Account::PrintBalance pAccount->Account::PrintBalance(); // Explicit qualification.
在前面的示例中,對 PrintBalance 的調(diào)用將禁用虛函數(shù)調(diào)用機制。
單個繼承
在“單繼承”(繼承的常見形式)中,類僅具有一個基類。考慮下圖中闡釋的關(guān)系。
簡單單繼承關(guān)系圖
注意該圖中從常規(guī)到特定的進度。在大多數(shù)類層次結(jié)構(gòu)的設(shè)計中發(fā)現(xiàn)的另一個常見特性是,派生類與基類具有“某種”關(guān)系。在該圖中,Book 是一種 PrintedDocument,而 PaperbackBook 是一種 book。
該圖中的另一個要注意的是:Book 既是派生類(來自 PrintedDocument),又是基類(PaperbackBook 派生自 Book)。此類類層次結(jié)構(gòu)的框架聲明如下面的示例所示:
// deriv_SingleInheritance.cpp // compile with: /LD class PrintedDocument {}; // Book is derived from PrintedDocument. class Book : public PrintedDocument {}; // PaperbackBook is derived from Book. class PaperbackBook : public Book {};
PrintedDocument 被視為 Book 的“直接基”類;它是 PaperbackBook 的“間接基”類。差異在于,直接基類出現(xiàn)在類聲明的基礎(chǔ)列表中,而間接基類不是這樣的。
在聲明派生的類之前聲明從中派生每個類的基類。為基類提供前向引用聲明是不夠的;它必須是一個完整聲明。
在前面的示例中,使用訪問說明符 public。 成員訪問控制中介紹了公共的、受保護的和私有的繼承的含義。
類可用作多個特定類的基類,如下圖所示。
注意
有向非循環(huán)圖對于單繼承不是唯一的。它們還用于表示多重繼承關(guān)系圖。 多重繼承中對本主題進行了說明。
在繼承中,派生類包含基類的成員以及您添加的所有新成員。因此,派生類可以引用基類的成員(除非在派生類中重新定義這些成員)。當(dāng)在派生類中重新定義了直接或間接基類的成員時,范圍解析運算符 (::) 可用于引用這些成員。請看以下示例:
// deriv_SingleInheritance2.cpp // compile with: /EHsc /c #include <iostream> using namespace std; class Document { public: char *Name; // Document name. void PrintNameOf(); // Print name. }; // Implementation of PrintNameOf function from class Document. void Document::PrintNameOf() { cout << Name << endl; } class Book : public Document { public: Book( char *name, long pagecount ); private: long PageCount; }; // Constructor from class Book. Book::Book( char *name, long pagecount ) { Name = new char[ strlen( name ) + 1 ]; strcpy_s( Name, strlen(Name), name ); PageCount = pagecount; };
請注意,Book 的構(gòu)造函數(shù) (Book::Book) 具有對數(shù)據(jù)成員 Name 的訪問權(quán)。在程序中,可以創(chuàng)建和使用類型為 Book 的對象,如下所示:
// Create a new object of type Book. This invokes the // constructor Book::Book. Book LibraryBook( "Programming Windows, 2nd Ed", 944 ); ... // Use PrintNameOf function inherited from class Document. LibraryBook.PrintNameOf();
如前面的示例所示,以相同的方式使用類成員和繼承的數(shù)據(jù)和函數(shù)。如果類 Book 的實現(xiàn)調(diào)用 PrintNameOf 函數(shù)的重新實現(xiàn),則只能通過使用范圍解析 (Document) 運算符來調(diào)用屬于 :: 類的函數(shù):
// deriv_SingleInheritance3.cpp // compile with: /EHsc /LD #include <iostream> using namespace std; class Document { public: char *Name; // Document name. void PrintNameOf() {} // Print name. }; class Book : public Document { Book( char *name, long pagecount ); void PrintNameOf(); long PageCount; }; void Book::PrintNameOf() { cout << "Name of book: "; Document::PrintNameOf(); }
如果存在可訪問的明確基類,則可以隱式將派生類的指針和引用轉(zhuǎn)換為其基類的指針和引用。下面的代碼使用指針演示了此概念(相同的原則適用于引用):
// deriv_SingleInheritance4.cpp // compile with: /W3 struct Document { char *Name; void PrintNameOf() {} }; class PaperbackBook : public Document {}; int main() { Document * DocLib[10]; // Library of ten documents. for (int i = 0 ; i < 10 ; i++) DocLib[i] = new Document; }
在前面的示例中,創(chuàng)建了不同的類型。但是,由于這些類型都派生自 Document 類,因此存在對 Document * 的隱式轉(zhuǎn)換。因此,DocLib 是“異類列表”(其中包含的所有對象并非屬于同一類型),該列表包含不同類型的對象。
由于 Document 類具有一個 PrintNameOf 函數(shù),因此它可以打印庫中每本書的名稱,但它可能會忽略某些特定于文檔類型的信息(Book 的頁計數(shù)、HelpFile 的字節(jié)數(shù)等)。
注意
強制使用基類來實現(xiàn)函數(shù)(如 PrintNameOf)通常不是最佳設(shè)計。 虛函數(shù)提供其他設(shè)計替代方法。