const函數與非const函數在不同情況下的調用情況

看下面的例子,輸出什麼?

 

#include <iostream>
using namespace std;


class Good {
public:
	Good() { i = 0; }
	int f()
	{
		return i;
	};
	int f() const
	{
		return i+1;
	};
	int i;
};


int main()
{
	Good good;
	std::cout<< good.f() <<endl;
	cout<< good.f() <<endl;

	const Good good1;
	std::cout << good1.f() << endl;
	cout << good1.f() << endl;

    return 0;
}

實際輸出情況:

const 對象,調用後面帶const的函數。

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章