confused by references



 DEVELOP > c-Plus-Plus > confused by references

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "tarmat"
Date: 23 Nov 2003 05:48:24 AM
Object: confused by references
I'm confused by the following. I'd appreciate any advice.
//-------------------main.cpp
#include <iostream>
using namespace std;
class A
{
public:

int val;
A(int v):val(v){}
};
/////////////////////////////////////////////
class MyClass
{
private:
A& a;
public:
MyClass(A& a_):a(a_){}
int Get(){return a.val;}
};
///////////////////////////////////////////
int main()
{
MyClass mc(A(1));
cout << "\nmc.a is now " << mc.Get();
//how can mc.a be pointing to a valid integer? Isn't A(1) a
temporary?

return 0;
}
Also, if the ctor of MyClass is changed to
MyClass(A a_):a(a_){}
I get the expected result (the reference is pointing to gobbldygook).
I don't understand why, given the behavior of the first example, this
should be so.
thanks
.

User: "Rob Williscroft"

Title: Re: confused by references 23 Nov 2003 07:58:58 AM
tarmat wrote in news:0771svgsd8ehmreb0ek4g83ctqpu4eblm7@4ax.com:

class MyClass
{
private:

A& a;

public:

MyClass(A& a_):a(a_){}

int Get(){return a.val;}

};

///////////////////////////////////////////
int main()
{
MyClass mc(A(1));

cout << "\nmc.a is now " << mc.Get();

//how can mc.a be pointing to a valid integer? Isn't A(1) a
temporary?

return 0;
}


Also, if the ctor of MyClass is changed to

MyClass(A a_):a(a_){}

I get the expected result (the reference is pointing to gobbldygook).
I don't understand why, given the behavior of the first example, this
should be so.


Your first example relies on a *non-standard* feature. you are binding
a non-const reference to temporary, MS VC6 allows this and so do other
compilers that choose to operate in a VC6-compatible mode, you may be
able to give your compiler an option to increase conformance to The
C++ Standard (for e.g /Za (MS VC7.1), -pedantic and -ansi (gcc)) also
turn your compiler warnings all the way up (often -Wall).
Once you change MyClass to:
class MyClass
{
private:
A const & a;
public:
MyClass(A const & a_):a(a_){}
int Get(){return a.val;}
};
Then your example will be standard conforming, however it will also
exhibit "Undefined behaviour" (aka UB). As you correctly sumize the
temporary is destroyed at the end of the statement "MyClass mc(A(1));".
This means that its destructor (if any) will be called and the compiler
may reclaime or reuse the sorage it occupied.
In this case *Nothing* happens, the compiler doesn't release or reuse
the storage for your temporary and A's destuctor does nothing.
Note the above paragraph is pure speculation, once you write code that
exhibits UB the C++ Standard washes it hands of *all* responsability
for what happens.
Rob.
--
http://www.victim-prime.dsl.pipex.com/
.


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER