none
Visual C++ Compiler does not create a temporary at int(i) RRS feed

  • Frage

  • hey folks,

    the following sourcecode reproduces the bug:

    #include <iostream>
    using namespace std;
    
    
    int i = 5;
    
    
    void fkt(const int& ref)
    {
        cout << '\n' << "address of temporary: " << &ref;
    
        cout << '\n' << "value of temporary: " << ref;
        i = 10;
        cout << '\n' << "value of temporary: " << ref;
    }
    
    
    int main()
    {
        cout << '\n' << "address of i        : " << &i;
    
        fkt( int(i) );
    }

    Bug: The reference "ref" does not bind a temporary (like it should be) but the global variable "i".
    Samstag, 30. Juni 2012 16:32

Antworten

  • Hallo erstmal....du bist hier in ein deutschen Forum also kannst du auch in deutsch Schreiben.

    P.S. Du schreibst ins falsche Forum... es gibt ein c++ Forum.

    Der Parameter ref wird auch nichts temporäres. Mit

    void fkt(const int& ref)

    hast du eine Referenz auf i erzeugt. Also sobald du den wert von ref änderst änderst du auch gleichzeitig i.

    Wenn du ein temporären Wert haben möchtest dann bräuchtest du sowas wie:

    void fkt(const int ref)
    {
        cout << '\n' << "address of temporary: " << &ref;
    
        cout << '\n' << "value of temporary: " << ref;
        i = 10;
        cout << '\n' << "value of temporary: " << ref;
        i=ref; // Schreibt den wert von ref nach i
    }


    Sonntag, 1. Juli 2012 16:59