Returning reference to member object

Discussion in 'C++' started by tech, Jun 18, 2008.

  1. tech

    tech Guest

    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;
    };
    tech, Jun 18, 2008
    #1
    1. Advertising

  2. On Jun 18, 1:32 pm, 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;
    >         };


    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
    Saeed Amrollahi, Jun 18, 2008
    #2
    1. Advertising

  3. On 18 Cze, 12:32, tech <> 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;
    > };
    Krzysztof Czaiñski, Jun 18, 2008
    #3
  4. -----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-----
    Federico Zenith, Jun 18, 2008
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ce
    Replies:
    1
    Views:
    2,982
  2. Replies:
    2
    Views:
    569
    Mark P
    May 9, 2005
  3. Naren
    Replies:
    4
    Views:
    393
    Victor Bazarov
    Jun 8, 2006
  4. JustMe
    Replies:
    1
    Views:
    169
    Tassilo v. Parseval
    Aug 29, 2003
  5. A. Farber
    Replies:
    5
    Views:
    171
    A. Farber
    Aug 23, 2008
Loading...

Share This Page