simple reference question

S

shaun roe

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
 
A

Achintya

shaun said:
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


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.
 
B

ben

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' ?

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top