新聞中心
C++編程語言可以被看做是C語言的升級(jí)版本,它能夠支持C語言中的所有功能,而且在其他方面也有很大的提升。其中,在C++操作符重載中++,--需要說明是++(--)在操作數(shù)前面,還是在操作數(shù)后面,區(qū)別如下:

創(chuàng)新互聯(lián)公司是一家集網(wǎng)站建設(shè),四平企業(yè)網(wǎng)站建設(shè),四平品牌網(wǎng)站建設(shè),網(wǎng)站定制,四平網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營銷,網(wǎng)絡(luò)優(yōu)化,四平網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競爭力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶成長自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。
C++操作符重載代碼經(jīng)過測試無誤(起碼我這里沒問題^_^)
- #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_;
- }
- // ++在前的模式,這里返回的是引用 ,準(zhǔn)許++++A
- A& operator ++ (){
- (this->m_)++;
- return *this;
- }
- // ++ 在后,這里返回的是一個(gè)新的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;
- }
以上就是對(duì)C++操作符重載的相關(guān)介紹。
網(wǎng)頁名稱:C++操作符重載不同方式區(qū)別
URL分享:http://www.5511xx.com/article/dphpijo.html


咨詢
建站咨詢
