C++ 学习之函数重载、基于const的重载
今天閱讀boost源碼發現一個奇怪的問題(enable_shared_from_this.hpp源碼)
#ifndef BOOST_SMART_PTR_ENABLE_SHARED_FROM_THIS_HPP_INCLUDED #define BOOST_SMART_PTR_ENABLE_SHARED_FROM_THIS_HPP_INCLUDED// // enable_shared_from_this.hpp // // Copyright 2002, 2009 Peter Dimov // // Distributed under the Boost Software License, Version 1.0. // See accompanying file LICENSE_1_0.txt or copy at // http://www.boost.org/LICENSE_1_0.txt // // http://www.boost.org/libs/smart_ptr/enable_shared_from_this.html //#include <boost/smart_ptr/weak_ptr.hpp> #include <boost/smart_ptr/shared_ptr.hpp> #include <boost/assert.hpp> #include <boost/config.hpp>namespace boost {template<class T> class enable_shared_from_this { protected:enable_shared_from_this() BOOST_NOEXCEPT{}enable_shared_from_this(enable_shared_from_this const &) BOOST_NOEXCEPT{}enable_shared_from_this & operator=(enable_shared_from_this const &) BOOST_NOEXCEPT{return *this;}~enable_shared_from_this() BOOST_NOEXCEPT // ~weak_ptr<T> newer throws, so this call also must not throw{}public:shared_ptr<T> shared_from_this(){shared_ptr<T> p( weak_this_ );BOOST_ASSERT( p.get() == this );return p;}shared_ptr<T const> shared_from_this() const{shared_ptr<T const> p( weak_this_ );BOOST_ASSERT( p.get() == this );return p;}public: // actually private, but avoids compiler template friendship issues// Note: invoked automatically by shared_ptr; do not calltemplate<class X, class Y> void _internal_accept_owner( shared_ptr<X> const * ppx, Y * py ) const{if( weak_this_.expired() ){weak_this_ = shared_ptr<T>( *ppx, py );}}private:mutable weak_ptr<T> weak_this_; };} // namespace boost#endif // #ifndef BOOST_SMART_PTR_ENABLE_SHARED_FROM_THIS_HPP_INCLUDED請注意,shared_from_this有兩個版本,其中一個后面有const,這時心中頓生疑問,難道這樣也可以重載?瞬間疑惑,寫個例子吧 #include "stdafx.h" #include <iostream> #include <deque>using namespace std;class Test { public:Test (int t):m_int(t) {}int getInt(int t){return m_int;}int getInt( int t) const{return m_int*2;}private:int m_int; };void main() {Test t(10);int ti = t.getInt(0);return ; } 編譯一下確實可以編譯的過去,調試一下發下ti=10,也就是說成功調用了非const版本的成員函數,這時心中更加疑慮,那個帶const怎么調用呀?百思不得其解就上網搜了下,果然C++支持這種寫法,調用帶const的成員函數的方式為: const Test *p = new Test(10);int ti = p->getInt(0);調試ti的值為20(當然用引用的方法也是一樣的)
這里是進行了函數重載,如果去掉非const的那個函數,用非const類型一樣可以直接調到帶const的那個函數.
下面是從網上得到的資料
函數重載
函數重載的定義是:在相同的作用域中,如果函數具有相同名字而僅僅是形參表不同,此時成為函數重載。注意函數重載不能基于不同的返回值類型進行重載。
注意函數重載中的“形參表”不同,是指本質不同,不要被一些表象迷惑。main函數不能被重載。
下面三組定義本質是相同的,不是重載:
1)int sum (int &a); 和 int sum (int &);
2)? int sum (int a) 和 int sum (const int a);
3)typedef int DD;
???? int sum(int a); 和 int sum (DD a);
其中第二個例子對于非引用傳參,形參是否const是等價的。但是當使用引用傳參時,有無const是不同的。使用指針傳參時,指向const對象的指針和指向非const對象的指針做形參的函數是不同的。
*下面談論一個比較惡心的問題,基于const的重載。
在類中會有這樣一種重載,它是合法的。
Class A {
int function ();
int function () const;
};
可以看到在A類中,function函數是發生重載了,而且是合法的。而且在調用時,只用A類的const對象才能調用const版本的function函數,而非const對象可以調用任意一種(調用const版本的該是先轉化為const對象),通常非const對象調用不是const版本的function函數。
原因是:按照函數重載的定義,函數名相同而形參表有本質不同的函數稱為重載。在類中,由于隱含的this形參的存在,const版本的 function函數使得作為形參的this指針的類型變為指向const對象的指針,而非const版本的使得作為形參的this指針就是正常版本的指針。此處是發生重載的本質。重載函數在最佳匹配過程中,對于const對象調用的就選取const版本的成員函數,而普通的對象調用就選取非const版本的成員函數。
(注:this指針是一個const指針,地址不能改,但能改變其指向的對象或者變量)
在類的成員函數后面加 const 還有(出了表明這個函數不會對這個類對象的數據成員(準確地說是非靜態數據成員)作任何改變。)什么好處呢?那就是常量(即 const)對象可以調用 const 成員函數,而不能調用非const修飾的函數。正如非const類型的數據可以給const類型的變量賦值一樣,反之則不成立。
總結
以上是生活随笔為你收集整理的C++ 学习之函数重载、基于const的重载的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【Boost】boost库中的小工具en
- 下一篇: 棋牌游戏服务器架构: 总体设计