Returning reference to member object

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • tech

    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().NonCon stFoo()
    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;
    };
  • Saeed Amrollahi

    #2
    Re: Returning reference to member object

    On Jun 18, 1:32 pm, tech <naumansulai... @googlemail.com wrote:
                   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().NonCon stFoo()
                  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().Chan ge();
    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

    Comment

    • =?ISO-8859-2?Q?Krzysztof_Czai=F1ski?=

      #3
      Re: Returning reference to member object

      On 18 Cze, 12:32, tech <naumansulai... @googlemail.com wrote:
      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;
      };

      Comment

      • Federico Zenith

        #4
        Re: Returning reference to member object

        -----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().NonCon stFoo()
        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

        iD8DBQFIWPkZBIp u+y7DlLcRAvs/AJ90Q/3R9GNrINy/T5iv1EaDrmDHJgC fTfV7
        KHG7S1uL/5mMYJuHuIS32lE=
        =cW54
        -----END PGP SIGNATURE-----

        Comment

        • Stuart Golodetz

          #5
          Re: Returning reference to member object

          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().NonCon stFoo()
          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

          Comment

          Working...