473,404 Members | 2,137 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

simple reference question

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
Jul 23 '05 #1
2 1348

shaun roe wrote:
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.

Jul 23 '05 #2
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


cheers

shaun

Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Michael B Allen | last post by:
Hi, I've tried to write the *simplest* memory allocator possible. I think it would be useful in many cases such as allocating memory on stack as a poor man's garbage collection perhaps. I was...
2
by: Peter | last post by:
Hello Thank for reviewing this question. I would like to know why I am getting a syntax error. For example public class public class B : public
2
by: Mike D Sutton | last post by:
Please excuse the terribly 'newbie'ness of this question, unfortunately my C# is very rusty.. What I'm trying to do is write a simple interactive drawing application where a few lines can be moved...
73
by: Claudio Grondi | last post by:
In the process of learning about some deeper details of Python I am curious if it is possible to write a 'prefix' code assigning to a and b something special, so, that Python gets trapped in an...
4
by: Armand | last post by:
Hi Guys, I have a set of array that I would like to clear and empty out. Since I am using "Array" not "ArrayList", I have been struggling in finding the solution which is a simple prob for those...
176
by: nw | last post by:
Hi, I previously asked for suggestions on teaching testing in C++. Based on some of the replies I received I decided that best way to proceed would be to teach the students how they might write...
30
by: galiorenye | last post by:
Hi, Given this code: A** ppA = new A*; A *pA = NULL; for(int i = 0; i < 10; ++i) { pA = ppA; //do something with pA
5
by: fussfart | last post by:
I'm trying to do something that should be very simple but isn't working! (I also want to do something somewhat more complicated, but that has to wait until I figure out the simple stuff.) First, I...
26
by: optimistx | last post by:
A variable in global scope var a1 = 'contents of global variable a1'; can be references (with some limitations) as window; // or window.a1; // or even window;
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.