473,385 Members | 2,269 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,385 software developers and data experts.

return reference to member variable

Hi,

I would like to return a reference to a member variable:

class foo
{
int i;
int& geti() const;
};

int& foo::geti() const
{
return i;
}

But I get the following error:
invalid initialization of reference of type 'double&' from expression
of type 'const double'

Am I missing something? This seems like it should be doable.

-Billy

Jul 23 '05 #1
5 2248
Billy wrote:
Hi,

I would like to return a reference to a member variable:

class foo
{
int i;
int& geti() const;
};

int& foo::geti() const
{
return i;
}

But I get the following error:
invalid initialization of reference of type 'double&' from expression
of type 'const double'

Am I missing something?


Isn't that error message obvious? Your member function is const, and so
within it, i is const. You're trying to bind it to a non-const referernce,
which is of course not allowed. Make it:

const int& foo::geti() const
{
return i;
}

Jul 23 '05 #2
I was under the impression that making the function const meant:
"within this function I promise not to change the state of the object"

Since I wasn't changing the state of the object I thought it should be
ok. Clearly my understanding of making a function const was overly
naive.

Thanks, Billy

Jul 23 '05 #3
Billy wrote:

I was under the impression that making the function const meant:
"within this function I promise not to change the state of the object"
Right.
But if you give away a reference to some object internals, then any
other lousy code *is* able to change the state of the object. Thus
the fact of handing out a reference opens the wormhole.

Since I wasn't changing the state of the object


You didn't. But you enabled other code to do it.
--
Karl Heinz Buchegger
kb******@gascad.at
Jul 23 '05 #4
Billy wrote:
I was under the impression that making the function const meant:
"within this function I promise not to change the state of the object"


"... and not to expose it in a way that makes changes to it from outside
possible".

Think of something like:

foo f;
f.geti()++;

Jul 23 '05 #5
Sorry I haven't done much programming...

Example
**********
foo f;
f.geti()++;

int& foo::geti(); // Example works
int& foo::geti() const; // Error, exposing foo via return
const int& foo::geti() const; // Error, f.geti() is const
const int& foo::geti(); // Error, f.geti() is const

Am I thinking about this right?

-Thanks, b

Jul 23 '05 #6

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

Similar topics

8
by: DaKoadMunky | last post by:
Please consider the following... <CODE> #include <string> using namespace std; typedef int PrimitiveType; typedef string ClassType;
5
by: Neal Coombes | last post by:
Posted to comp.lang.c++.moderated with little response. Hoping for better from the unmoderated groups: -------- Original Message -------- Subject: Return appropriately by value, (smart)...
5
by: jmd | last post by:
hello. i am trying VC++ 7.1 (with vsnet 2003). my example : class Complex { public: Complex ( float re_ = 0.0, float im_ = 0.0 ) : re(re_), im(im_) {} float Re () { return ( re ); } float...
20
by: weston | last post by:
I've got a piece of code where, for all the world, it looks like this fails in IE 6: hometab = document.getElementById('hometab'); but this succeeds: hometabemt =...
12
by: huangshan | last post by:
hi all In what condition i need( or mast) a (templates)function return value by reference? can you give me a example thanks
6
by: Bo Yang | last post by:
I know the below code is wrong: Object & test(){ return Object() ;} Object & v = test() ; the Object is just a temporary variable, so you the variable may be reference to nothing. And I...
8
by: Bart Simpson | last post by:
If a class has a member variable that is a reference. What happens to teh class that is being referenced, when the containing class is destroyed? e.g. Class A{ }; Class B { B(const A&...
10
by: flopbucket | last post by:
Hi, Is this legal? std::string foo() { std::string xyz = "FOO"; return xyz; }
68
by: Jim Langston | last post by:
I remember there was a thread a while back that was talking about using the return value of a function as a reference where I had thought the reference would become invalidated because it was a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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.