Connecting Tech Pros Worldwide Forums | Help | Site Map

simple reference question

shaun roe
Guest
 
Posts: n/a
#1: Jul 23 '05
A basic question, inspired by code in 'exceptional c++ style' pg. 124:

The example code illustrates accessor functions instead of making data
public.
To paraphrase:

Class X {
public:
T1& UseT1() {return t1_;}
private:
T1 t1_;
};

I would have written: T1 UseT1() {return t1_;}, so my question is:

t1_ is just returned (as a value?) so what does the '&' do in the return
type definition? Does it really return a reference to the internal
value? What are the ownership/lifetime implications of 'T1 &' versus
'T1' ?

cheers

shaun

Achintya
Guest
 
Posts: n/a
#2: Jul 23 '05

re: simple reference question



shaun roe wrote:[color=blue]
> A basic question, inspired by code in 'exceptional c++ style' pg.[/color]
124:[color=blue]
>
> The example code illustrates accessor functions instead of making[/color]
data[color=blue]
> public.
> To paraphrase:
>
> Class X {
> public:
> T1& UseT1() {return t1_;}
> private:
> T1 t1_;
> };
>
> I would have written: T1 UseT1() {return t1_;}, so my question is:
>
> t1_ is just returned (as a value?) so what does the '&' do in the[/color]
return[color=blue]
> type definition? Does it really return a reference to the internal
> value? What are the ownership/lifetime implications of 'T1 &' versus
> 'T1' ?
>
> cheers
>
> shaun[/color]


Hi,

Yes It really returns the reference of the private member t1_

Still class X is the owner and lifetime of t1_ is dependent on the life
time of objects of X.

To be more clear ...using references one cannot delete the memory.
Because the variable to which the assigment would be made will be of
type reference to T1 and not a pointer type.

-vs_p.

ben
Guest
 
Posts: n/a
#3: Jul 23 '05

re: simple reference question



[color=blue]
> A basic question, inspired by code in 'exceptional c++ style' pg. 124:
>
> The example code illustrates accessor functions instead of making data
> public.
> To paraphrase:
>
> Class X {
> public:
> T1& UseT1() {return t1_;}
> private:
> T1 t1_;
> };
>
> I would have written: T1 UseT1() {return t1_;}, so my question is:
>
> t1_ is just returned (as a value?) so what does the '&' do in the return
> type definition? Does it really return a reference to the internal
> value? What are the ownership/lifetime implications of 'T1 &' versus
> 'T1' ?[/color]

1. The whole point of the declared function to enable something like this:

X x;
T1& t = x.UseT1(); // get value;
x.UseT1() = T(); // set value;
t = T(); // another way to set value;

Had the return type be a plain T1, you won't be able to set the value.

2. Now since X::t1_ is owned by X, its life time relies on X, so

X& x_ref = *(new X);
T1& t = x_ref.UseT1();
delete &x_ref;

t = T(); // funny things happen here because t references to no object
now

3. For completion, it is nice to add one more UseT1 for const use:

const T1& UseT1() const;

so to enable something like this:

const X x;
T1 t = x.UseT1(); // OK, from const T1& to T1
x.UseT1() = T(); // ERROR, cannot assign a constant

[color=blue]
>
> cheers
>
> shaun[/color]


Closed Thread