VC++ Compiler Defect? - Programmer

This is a discussion on VC++ Compiler Defect? - Programmer ; I have boiled down a problem that we having with an application. It appears that the VC++ compiler does not set a reference variable when called for a second time. We can work around it; but, would really like to ...

+ Reply to Thread
Results 1 to 4 of 4

Thread: VC++ Compiler Defect?

  1. VC++ Compiler Defect?

    I have boiled down a problem that we having with an application. It
    appears that the VC++ compiler does not set a reference variable when
    called for a second time. We can work around it; but, would really
    like to know if this is a compiler defect and if so, exactly what do
    we need to look for.

    The following 25 lines, which can be placed into a single file command
    line project, will demonstrate the problem. When reusing the rLong
    variable for the second time, it is not set to the address of the new
    object. Optimization has been turned off and stepping through the
    code indicates proper operation with the exception of the address
    returned the second time for rLong. The result is consistent between
    VC6 and VC2003.

    Thanks for any insight.

    -------------------------------------
    #include

    class CTestOb {
    public:
    CTestOb(long lValue){m_lValue=lValue;};
    virtual ~CTestOb(){};
    long &GetRef(){return m_lValue;};
    long m_lValue;
    };

    int main(int argc, char* argv[])
    {
    CTestOb testob1(1);
    long &rLong = testob1.GetRef();
    printf( "Ref Address for Ob1 = %X and Value = %ld\r\n", &rLong,
    rLong);

    // Address correctly obtained for new rLong2 value
    CTestOb testob2(2);
    long &rLong2 = testob2.GetRef();
    printf( "Address for Renamed Reference for Ob2 = %X and Value =
    %ld\r\n", &rLong2, rLong2);

    // Address not reset for rLong - still set to testob1.GetRef()
    value
    rLong = testob2.GetRef();
    printf( "Ref Address for Ob2 = %X and Value = %ld\r\n", &rLong,
    rLong);

    return 0;
    }

  2. Re: VC++ Compiler Defect?

    No, it's just C++.

    Page 98, "The C++ Programming Language" 3rd edition, by B. Stroustrup reads:

    "...the value of a reference cannot be changed after initialization; it
    always refers to the object it was initialized to denote."

    It's strange that you didn't see any warnings during compilation. Did you
    try Warning level 4?

    Boris

    "M. Todd Matthews" wrote in message
    news:b0efe3e.0310161421.3546ba1@posting.google.com ...
    > I have boiled down a problem that we having with an application. It
    > appears that the VC++ compiler does not set a reference variable when
    > called for a second time. We can work around it; but, would really
    > like to know if this is a compiler defect and if so, exactly what do
    > we need to look for.
    >
    > The following 25 lines, which can be placed into a single file command
    > line project, will demonstrate the problem. When reusing the rLong
    > variable for the second time, it is not set to the address of the new
    > object. Optimization has been turned off and stepping through the
    > code indicates proper operation with the exception of the address
    > returned the second time for rLong. The result is consistent between
    > VC6 and VC2003.
    >
    > Thanks for any insight.
    >
    > -------------------------------------
    > #include
    >
    > class CTestOb {
    > public:
    > CTestOb(long lValue){m_lValue=lValue;};
    > virtual ~CTestOb(){};
    > long &GetRef(){return m_lValue;};
    > long m_lValue;
    > };
    >
    > int main(int argc, char* argv[])
    > {
    > CTestOb testob1(1);
    > long &rLong = testob1.GetRef();
    > printf( "Ref Address for Ob1 = %X and Value = %ld\r\n", &rLong,
    > rLong);
    >
    > // Address correctly obtained for new rLong2 value
    > CTestOb testob2(2);
    > long &rLong2 = testob2.GetRef();
    > printf( "Address for Renamed Reference for Ob2 = %X and Value =
    > %ld\r\n", &rLong2, rLong2);
    >
    > // Address not reset for rLong - still set to testob1.GetRef()
    > value
    > rLong = testob2.GetRef();
    > printf( "Ref Address for Ob2 = %X and Value = %ld\r\n", &rLong,
    > rLong);
    >
    > return 0;
    > }




  3. Re: VC++ Compiler Defect?

    tntmatthews@hotmail.com (M. Todd Matthews) wrote in message news:...
    > I have boiled down a problem that we having with an application. It
    > appears that the VC++ compiler does not set a reference variable when
    > called for a second time. We can work around it; but, would really
    > like to know if this is a compiler defect and if so, exactly what do
    > we need to look for.
    >
    > The following 25 lines, which can be placed into a single file command
    > line project, will demonstrate the problem. When reusing the rLong
    > variable for the second time, it is not set to the address of the new
    > object. Optimization has been turned off and stepping through the
    > code indicates proper operation with the exception of the address
    > returned the second time for rLong. The result is consistent between
    > VC6 and VC2003.
    >
    > Thanks for any insight.
    >
    > -------------------------------------
    > #include
    >
    > class CTestOb {
    > public:
    > CTestOb(long lValue){m_lValue=lValue;};
    > virtual ~CTestOb(){};
    > long &GetRef(){return m_lValue;};
    > long m_lValue;
    > };
    >
    > int main(int argc, char* argv[])
    > {
    > CTestOb testob1(1);
    > long &rLong = testob1.GetRef();
    > printf( "Ref Address for Ob1 = %X and Value = %ld\r\n", &rLong,
    > rLong);
    >
    > // Address correctly obtained for new rLong2 value
    > CTestOb testob2(2);
    > long &rLong2 = testob2.GetRef();
    > printf( "Address for Renamed Reference for Ob2 = %X and Value =
    > %ld\r\n", &rLong2, rLong2);
    >
    > // Address not reset for rLong - still set to testob1.GetRef()
    > value
    > rLong = testob2.GetRef();
    > printf( "Ref Address for Ob2 = %X and Value = %ld\r\n", &rLong,
    > rLong);
    >
    > return 0;
    > }



    Upon further investigation, the compiler works as it should;
    references are tricky and Stroustrup explains what we see in "The C++
    Programming Language, Third Edition", pg 97 -
    "Initialization is something quite different from assignment to it.
    Despite appearances, no operator operates on a reference. For
    example, ... Consequently, the value of a refefence cannot be changed
    after initialization; it always refers to the object it was
    initialized to denote."

  4. Re: VC++ Compiler Defect?

    Boris Dynin wrote:
    > It's strange that you didn't see any warnings during compilation.


    That's because there's nothing wrong with it, e.g.:

    int a = 10;
    int b = 20;
    int& ra = a; // create alias for 'a'
    int& ra = b; // create alias for 'b'
    ra = rb; // valid: assign b to a

    The only thing wrong is that the OP initially mistook the last
    assignment as a ref re-sitting (which, as he found out, is not
    permitted).


+ Reply to Thread