Returning reference to member object

T

tech

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;
};
 
S

Saeed Amrollahi

               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;
        };

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
 
F

Federico Zenith

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
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-----
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top