新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
C++操作符重載不同方式區(qū)別
C++編程語言可以被看做是C語言的升級版本,它能夠支持C語言中的所有功能,而且在其他方面也有很大的提升。其中,在C++操作符重載中++,--需要說明是++(--)在操作數(shù)前面,還是在操作數(shù)后面,區(qū)別如下:

創(chuàng)新互聯(lián)公司是一家集網(wǎng)站建設,四平企業(yè)網(wǎng)站建設,四平品牌網(wǎng)站建設,網(wǎng)站定制,四平網(wǎng)站建設報價,網(wǎng)絡營銷,網(wǎng)絡優(yōu)化,四平網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強企業(yè)競爭力。可充分滿足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時我們時刻保持專業(yè)、時尚、前沿,時刻以成就客戶成長自我,堅持不斷學習、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實用型網(wǎng)站。
C++操作符重載代碼經過測試無誤(起碼我這里沒問題^_^)
- #include < iostream>
- #include < cstdlib>
- using namespace std;
- template< typename T> class A
- {
- public:
- A(): m_(0){
- }
- // +
- const T operator + (const T& rhs)
- {
- // need to be repaired , but see it is only a demo
- return (this->m_ + rhs);
- }
- // -
- const T operator - (const T& rhs){
- // need to be repaired , but see it is only a demo
- return (this->m_ - rhs);
- }
- T getM(){
- return m_;
- }
- // ++在前的模式,這里返回的是引用 ,準許++++A
- A& operator ++ (){
- (this->m_)++;
- return *this;
- }
- // ++ 在后,這里返回的是一個新的A類型變量,且不可改變
- // 目的是防止出現(xiàn) A++++情況
- const A operator ++(int a){
- A< T> b = *this;
- (this->m_)++;
- return b;
- }
- private:
- T m_;
- };
- int main(void){
- int i = 0;
- cout< < ++++i< < endl;
- // i++++ is not allowed
- A< int> a;
- A< int> b = ++a;
- cout< < b.getM()< < endl;
- A< int> c = a++;
- cout< < c.getM()< < endl;
- cout< < a.getM()< < endl;
- int t = a+2;
- cout< < t< < endl;
- system("pause");
- return 0;
- }
以上就是對C++操作符重載的相關介紹。
分享文章:C++操作符重載不同方式區(qū)別
新聞來源:http://m.fisionsoft.com.cn/article/dphpijo.html


咨詢
建站咨詢
