473,386 Members | 1,745 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,386 software developers and data experts.

returning reference of a member variable

Hi ,
Is it correct to return reference of a member variable.
because it can be like we have already deleted the object and still
holding the reference
of member variable.
A *a = new A;
int & r = a.get(); // int& A::get() { //returning a member of class
A}
delete a;
cout<<r;

Jun 7 '06 #1
4 3241
Naren wrote:
Hi ,
Is it correct to return reference of a member variable.
Yes.
because it can be like we have already deleted the object and still
holding the reference of member variable.
Well, you have to ensure that this doesn't happen.
A *a = new A;
int & r = a.get(); // int& A::get() { //returning a member of class
A}
delete a;
cout<<r;


That would invoke undefined behavior.

Jun 7 '06 #2

Naren wrote:
Hi ,
Is it correct to return reference of a member variable.
because it can be like we have already deleted the object and still
holding the reference
of member variable.
A *a = new A;
int & r = a.get(); // int& A::get() { //returning a member of class
A}
delete a;
cout<<r;


Another problem with returning a non-const reference to a member is
that the client can then use it to change the value of the object's
member variable directly - instead of having to go through the proper
channels (which would be the object's class interface).

Greg

Jun 7 '06 #3

Rolf Magnus wrote:
Naren wrote:
...
A *a = new A;
int & r = a.get(); // int& A::get() { //returning a member of class
A}
delete a;
cout<<r;


That would invoke undefined behavior.


And what if I use
int const& r = a.get(); //int const& A::get()

will it still be undefined behavior?

Jun 8 '06 #4
ko*****@gmail.com wrote:
Rolf Magnus wrote:
Naren wrote:
...
A *a = new A;
int & r = a.get(); // int& A::get() { //returning a member of
class A}
delete a;
cout<<r;


That would invoke undefined behavior.


And what if I use
int const& r = a.get(); //int const& A::get()

will it still be undefined behavior?


What's different here? 'a' is not a temporary, A::get returns
a reference, not a temporary. As soon as 'a' is destroyed, all
of its contents is gone, and any reference to any part of 'a'
becomes invalid. Using it has UB.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 8 '06 #5

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

Similar topics

18
by: cppaddict | last post by:
Hi, Is it considered bad form to have the subscript operator return a const reference variable? If not, what is the proper way to do it? My question was prompted by the code below, my...
12
by: Olumide | last post by:
I'm studying Nigel Chapman's Late Night Guide to C++ which I think is an absolutely fantastic book; however on page 175 (topic: operator overlaoding), there the following code snippet: inline...
13
by: Matthias Kaeppler | last post by:
Hi, I was wondering why library implementors often make getter functions return strings by value (copies). For example, in boost::filesystem the leaf() function returns an std::string by value....
10
by: Fraser Ross | last post by:
I need to know the syntax for writing a reference of an array. I haven't seen it done often. I have a class with a member array and I want a member function to return an reference to it. ...
2
by: Cory Burkhardt | last post by:
I am not sure what the suggested practice is for returning reference types from read-only properties. Consider an example: Suppose I have a Point class that is a reference type. I use the Point...
8
by: Tim Clacy | last post by:
1) Is this initialising the reference 'u' to the address of the literal '2' or to the address 0x00000002? unsigned const& u = 2; 2) What is the different between the initialisation of 'u'...
17
by: djcredo | last post by:
Hey all, I want to return a pointer to a struct. Here is what I'lm trying to do: struct Position{ int x; int y; }; Position* GraphicTag::getRenderCentre(){
5
by: eriwik | last post by:
Consider this: #include <iostream> template<int A> struct test { const int& get() { return A; } } int main()
23
by: pauldepstein | last post by:
Below is posted from a link for Stanford students in computer science. QUOTE BEGINS HERE Because of the risk of misuse, some experts recommend never returning a reference from a function or...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...

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.