By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,276 Members | 1,410 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,276 IT Pros & Developers. It's quick & easy.

Returning reference to member object

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
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

P: n/a
-----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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.