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

Returning reference to member object

Hi, I want to return a reference to member object
provided in class Test, so have
provided a getter function GetObj() and declared it
const. However i'm getting a
compiler error that i need to return reference to const
obj but if i do do this
i can't call non const member functions eg
GetObj().NonConstFoo()
How do i get round this problem. I woul think that
GetObj is logically const as
it does not change the object.This is on MSVC++ V8.0

class Obj
{

};

class Test
{
public:
Obj& GetObj() const
{
return m_obj;
}

private:
Obj m_obj;
};
Jun 27 '08 #1
4 1740
On Jun 18, 1:32*pm, tech <naumansulai...@googlemail.comwrote:
* * * * * * * *Hi, I want to return a reference to memberobject
provided in class Test, so have
* * * * * * * provided a getter function GetObj() and declared it
const. However i'm getting a
* * * * * * * *compiler error that i need to return reference to const
obj but if i do do this
* * * * * * * i can't call non const member functions eg
GetObj().NonConstFoo()
* * * * * * * How do i get round this problem. I woul thinkthat
GetObj is logically const as
* * * * * * * *it does not change the object.This is on MSVC++ V8.0

* * * * * * * *class Obj
* * * * {

* * * * };

* * * * class Test
* * * * {
* * * * public:
* * * * * * * * Obj& GetObj() const
* * * * * * * * {
* * * * * * * * * * * * return m_obj;
* * * * * * * * }

* * * * private:
* * * * * * * * Obj m_obj;
* * * * };
Hi
const member functions is a kind member functions that you can't
change the state of object through them.
GetObj() is const. so you can't change the state of Test object (here
is m_Obj) via it. on the other hand
it returns the reference to data member. so accord to reference
concept it is ready to change. for example
you can call a non-const member function of class Obj:
class Obj {
void Change() { // ... } // non-const
};

Test t;
t.GetObj().Change();
clearly, this is a contradiction. So before any use Test class
compiler complains.
There are two workarounds:
1. If you intend to change the m_Obj, declare GetObj() as an ordinary
member function.
2. If you intend to return a copy of m_Obj, change the return type to
Obj (return the value of m_Obj)

Good luck
Regards,
Saeed Amrollahi
Jun 27 '08 #2
On 18 Cze, 12:32, tech <naumansulai...@googlemail.comwrote:
class Obj {};

class Test
{
public:
You need 2 versions of this method: for const and non-const use of the
returned reference:

const Obj& GetObj() const
{
return m_obj;
}
Obj& GetObj()
{
return m_obj;
}
>
private:
Obj m_obj;
};
Jun 27 '08 #3
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

tech wrote:
Hi, I want to return a reference to member object provided in class Test, so have
provided a getter function GetObj() and declared it const. However i'm getting a
compiler error that i need to return reference to const obj but if i do do this
i can't call non const member functions eg GetObj().NonConstFoo()
How do i get round this problem. I woul think that GetObj is logically const as
it does not change the object.This is on MSVC++ V8.0
Nope, it is _not_ logically const, because Obj is indeed a part of Test.
If you change Obj, you change Test as well.

You either have to return a const Obj&, or you do not declare GetObj to
be const, or you make both versions.

Cheers,
- -Federico
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (GNU/Linux)
Comment: Using GnuPG with SUSE - http://enigmail.mozdev.org

iD8DBQFIWPkZBIpu+y7DlLcRAvs/AJ90Q/3R9GNrINy/T5iv1EaDrmDHJgCfTfV7
KHG7S1uL/5mMYJuHuIS32lE=
=cW54
-----END PGP SIGNATURE-----
Jun 27 '08 #4
tech wrote:
Hi, I want to return a reference to member object
provided in class Test, so have
provided a getter function GetObj() and declared it
const. However i'm getting a
compiler error that i need to return reference to const
obj but if i do do this
i can't call non const member functions eg
GetObj().NonConstFoo()
How do i get round this problem. I woul think that
GetObj is logically const as
it does not change the object.This is on MSVC++ V8.0

class Obj
{

};

class Test
{
public:
Obj& GetObj() const
{
return m_obj;
}

private:
Obj m_obj;
};
I think you're a bit confused. When you make a member function const,
you're just saying that *this should be treated as const. So in this
case, the instance of Test on which GetObj is called gets treated as
const. In other words, *this is treated as being a const Test&. Given
that, this->m_obj is treated as a const Obj. You can't bind a non-const
reference to a const object, which is why you're getting an error.

As other people have said, you need some combination of:

Obj& GetObj();
const Obj& GetObj() const;

Regards,
Stu
Jun 27 '08 #5

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

Similar topics

9
by: mjm | last post by:
Folks, Stroustrup indicates that returning by value can be faster than returning by reference but gives no details as to the size of the returned object up to which this holds. My question is...
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...
5
by: Gent | last post by:
I have two questions which are very similar: Is it possible to return an object in C++. Below is part of my code for reference however I am more concerned about the concept. It seems like the...
11
by: JKop | last post by:
AnyClass Blah() { AnyClass poo; return poo; } As we all know, in the above, the compiler is entitled to:
11
by: anand | last post by:
I am an STL newbie trying to build a class DUTBus that has a map object. In my member function, I try to return a Map Object, however, it does not seem to work. I am referring to "Bus getBus()"...
4
by: Naren | last post by:
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...
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(){
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...
160
by: DiAvOl | last post by:
Hello everyone, Please take a look at the following code: #include <stdio.h> typedef struct person { char name; int age; } Person;
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...

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.