C++ 中const修飾虛函數(shù)實例詳解
更新時間:2017年06月01日 15:32:51 投稿:lqh
這篇文章主要介紹了C++ 中const修飾虛函數(shù)實例詳解的相關(guān)資料,需要的朋友可以參考下
C++ 中const修飾虛函數(shù)實例詳解
【1】程序1
#include <iostream> using namespace std; class Base { public: virtual void print() const = 0; }; class Test : public Base { public: void print(); }; void Test::print() { cout << "Test::print()" << endl; } void main() { // Base* pChild = new Test(); //compile error! // pChild->print(); }
【2】程序2
#include <iostream> using namespace std; class Base { public: virtual void print() const = 0; }; class Test : public Base { public: void print(); void print() const; }; void Test::print() { cout << "Test::print()" << endl; } void Test::print() const { cout << "Test::print() const" << endl; } void main() { Base* pChild = new Test(); pChild->print(); } /* Test::print() const */
【3】程序3
#include <iostream> using namespace std; class Base { public: virtual void print() const = 0; }; class Test : public Base { public: void print(); void print() const; }; void Test::print() { cout << "Test::print()" << endl; } void Test::print() const { cout << "Test::print() const" << endl; } void main() { Base* pChild = new Test(); pChild->print(); const Test obj; obj.print(); Test obj1; obj1.print(); Test* pOwn = new Test(); pOwn->print(); } /* Test::print() const Test::print() const Test::print() Test::print() */
備注:一切皆在代碼中。
總結(jié):const修飾成員函數(shù),也屬于函數(shù)重載的一種范疇。
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
C++實現(xiàn)班車管理系統(tǒng)課程設(shè)計
這篇文章主要為大家詳細介紹了C++實現(xiàn)班車管理系統(tǒng)課程設(shè)計,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-03-03C語言用函數(shù)實現(xiàn)電話簿管理系統(tǒng)
這篇文章主要為大家詳細介紹了C語言用函數(shù)實現(xiàn)電話簿管理系統(tǒng),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-12-12使用VS2022開發(fā)在線遠程編譯部署的C++程序(圖文詳解)
這篇文章主要介紹了使用VS2022開發(fā)可以在線遠程編譯部署的C++程序,本文分步驟通過圖文并茂的形式給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-12-12