C++ 中const修飾虛函數(shù)實(shí)例詳解
C++ 中const修飾虛函數(shù)實(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ù)重載的一種范疇。
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
相關(guān)文章
2~62位任意進(jìn)制轉(zhuǎn)換方法(c++)
下面小編就為大家?guī)硪黄?~62位任意進(jìn)制轉(zhuǎn)換方法(c++)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-06-06
C++實(shí)現(xiàn)LeetCode(119.楊輝三角之二)
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(119.楊輝三角之二),本篇文章通過簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07
在C++中高效使用和處理Json格式數(shù)據(jù)的示例代碼
最近的項(xiàng)目在用c處理后臺(tái)的數(shù)據(jù)時(shí),因?yàn)楹枚嗤獠拷涌诙荚谑褂肑son格式作為返回的數(shù)據(jù)結(jié)構(gòu)和數(shù)據(jù)描述,如何在c中高效使用和處理Json格式的數(shù)據(jù)就成為了必須要解決的問題,需要的朋友可以參考下2023-11-11
C++中priority_queue模擬實(shí)現(xiàn)的代碼示例
在c++語言中數(shù)據(jù)結(jié)構(gòu)中的堆結(jié)構(gòu)可以通過STL庫中的priority_queue 優(yōu)先隊(duì)列來實(shí)現(xiàn),這樣做極大地簡(jiǎn)化了我們的工作量,這篇文章主要給大家介紹了關(guān)于C++中priority_queue模擬實(shí)現(xiàn)的相關(guān)資料,需要的朋友可以參考下2021-08-08
c++利用stl set_difference對(duì)車輛進(jìn)出區(qū)域進(jìn)行判定
這篇文章主要介紹了set_difference,用于求兩個(gè)集合的差集,結(jié)果集合中包含所有屬于第一個(gè)集合但不屬于第二個(gè)集合的元素,需要的朋友可以參考下2017-03-03
C++利用棧實(shí)現(xiàn)中綴表達(dá)式轉(zhuǎn)后綴表達(dá)式
這篇文章主要為大家詳細(xì)介紹了C++利用棧實(shí)現(xiàn)中綴表達(dá)式轉(zhuǎn)后綴表達(dá)式,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-04-04
用C++類實(shí)現(xiàn)單向鏈表的增刪查和反轉(zhuǎn)操作方法
下面小編就為大家?guī)硪黄肅++類實(shí)現(xiàn)單向鏈表的增刪查和反轉(zhuǎn)操作方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-04-04

