• <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>
  • 關于傳遞臨時對象的一個問題

    發表于:2007-05-25來源:作者:點擊數: 標簽:問題臨時傳遞對象一個
    通過常量引用過來的對象不能調用成元函數.否則會出現類似:'function' : number overloads have no legal conversions for 'this' pointer的錯誤, 除了刪掉參數中的const外,還有什么解決辦法嗎? 代碼: #include stdafx.h #include class X { int i; public:
    通過常量引用過來的對象不能調用成元函數.否則會出現類似:'function' : number overloads have no legal conversions for 'this' pointer的錯誤,
    除了刪掉參數中的const外,還有什么解決辦法嗎?

    代碼:
    #include "stdafx.h"
    #include
    class X {
    int i;
    public:
    X();
    int x();
    void x(int x);
    };

    X::X()
    {
    i = 1;
    }

    int X::x()
    {
    return i;
    }

    void X::x(int x)
    {
    i = x;
    }

    X f()
    {
    return X();
    }

    void g1(X& x)
    {
    // cout << " non-const reference:" << x.x() << endl;
    }

    void g2(const X& x)
    {
    // cout << " const reference:" << ends << "get x.i:" << x.x() << endl;

    // int temp =3;
    // cout << "set x.i:" << temp << endl;
    // x.x(temp);
    // cout << "get x.i:" << x.x() << endl;
    }

    int main(int argc, char* argv[])
    {
    g1(f());
    g2(f());

    return 0;
    }

    將成元函數改成const的然后調用:
    #include "stdafx.h"
    #include
    class X {
    int i;
    public:
    X();
    int x()
    const ;
    void x(int x);
    };

    X::X()
    {
    i = 1;
    }

    int X::x()
    const
    {
    return i;
    }
    void X::x(int x)
    {
    i = x;
    }
    X f()
    {
    return X();
    }

    void g1(X& x)
    {
    cout << " non-const reference:" << x.x() << endl;
    }

    void g2(const X& x)
    {
    cout << " const reference:" << ends << "get x.i:" << x.x() << endl;

    // int temp =3;
    // cout << "set x.i:" << temp << endl;
    // x.x(temp);
    // cout << "get x.i:" << x.x() << endl;
    }

    int main(int argc, char* argv[])
    {
    g1(f());
    g2(f());

    return 0;
    }

    注:如果聲明一個成員函數為const函數,則等于告訴編譯器可以為一個const對象調用這個函數。非
    const對象也可以調用這個函數,為保證函數的常量性,在函數定義中,如果我們改變對象中的任何成員或調用任何非const成元函數,編譯器都會報錯
    即改為如下是不對的:
    #include "stdafx.h"
    #include <iostream.h>
    class X {
        int i;
    public:
        X();
         int x() const;
        void x(int x) const ;
    };

    X::X()
    {
        i = 1;
    }

      int X::x() const
    {
        return i;
    }
     void X::x(int x)  const
    {
        i = x;
    }
    X f()
    {
        return X();
    }

    void g1(X& x)
    {
        cout << " non-const reference:" << x.x() << endl;
    }

    void g2(const X& x)
    {
        cout << " const reference:" << ends << "get x.i:" << x.x() << endl;
       
        int temp =3;
        cout << "set x.i:" << temp << endl;
    //    x.x(temp);
        cout << "get x.i:" << x.x() << endl;
    }

        int main(int argc, char* argv[])
    {
            g1(f());
            g2(f());

        return 0;
    }


    原文轉自:http://www.kjueaiud.com

    評論列表(網友評論僅供網友表達個人看法,并不表明本站同意其觀點或證實其描述)
    老湿亚洲永久精品ww47香蕉图片_日韩欧美中文字幕北美法律_国产AV永久无码天堂影院_久久婷婷综合色丁香五月

  • <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>