Thread: Style of getters

  1. #1
    template<typename T> threahdead's Avatar
    Join Date
    Sep 2002
    Posts
    214

    Style of getters

    Hello,

    with the const correctness chapter of the C++ FAQ in mind, i think about redesigning my getters.

    I have the following code example:
    Code:
    template<typename T>
    class Container
    {
    public:
        // prefer this one?
        T getter1(const int index) const
        {
            return data[index];
        }
    
        // or this one by const ref?
        T const &getter2(const int index) const
        {
            return data[index];
        }
    
    private:
        T *data;
    };
    Is it genereally more appropriate to return by const reference or by value?

    As much as i know, a reference can never be NULL, so if my data might be NULL in any point in the data array, i should rather return by value. Is this the only criteria to choose between the two?

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by threahdead View Post
    Is it genereally more appropriate to return by const reference or by value?
    It seems a bit zealous to apply const correctness to a getter so that it returns a reference when it would otherwise return by value, since changing the returned value would not affect the object value anyway.

    Also, why not just return by const value??? In that case, you get the type safety of const correctness, and you can return NULL. The use of reference vs. value seems like a red herring (ie, irrelevant) WRT const correctness...
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > so if my data might be NULL in any point in the data array, i should rather return by value.
    Would it have a good reason to be NULL?
    Perhaps you ought to throw an exception instead, if that represents say an "out of bounds" access.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    Quote Originally Posted by MK27 View Post
    Also, why not just return by const value??? In that case, you get the type safety of const correctness, and you can return NULL. The use of reference vs. value seems like a red herring (ie, irrelevant) WRT const correctness...
    Returning by const value does not make sense. Why you would need const correctness in an object which will be destroyed anyway. The reason to return by const reference is to avoid unnecessary copying, for example, when your getter returns a string and you only need its length. You have to keep in mind, that you should not store pointer to this object and to treat this referenced object like an R-value.

  5. #5
    template<typename T> threahdead's Avatar
    Join Date
    Sep 2002
    Posts
    214
    Thanks for all your input!

    Actually i was a bit zealous to change everything to const correctness after reading about it. This doesn't make sense in this case where my getters should be returning by value.

    Best regards

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by kmdv View Post
    Returning by const value does not make sense.
    True. If you are returning a reference or a pointer, of course that should be const. However, I do not think the concept of "const correctness" means that you should change your return by value methods into const references, which (it seems to me) was the basis of the OP's question -- in that case, you might as well use const values. One silly idea deserves another
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    1. How can you return NULL? The thing you're returning is T, you have no idea what T is, who said T could be NULL?
    2. Again, you have no idea what T is -- what if it's massively expensive to copy it?

    Jeez, how hard is it to look at std::vector and see what the experts do? Return by CONST REFERENCE.

    EDIT: To be more explicit about it, the NULL thing is a non-issue. If T is a pointer type, and an element is NULL, that has nothing to do with returning a reference. You're returning a reference to a pointer, the pointer happens to be NULL, there is no problem here. NULL has nothing to do with anything as far as this decision goes.
    Last edited by brewbuck; 04-28-2011 at 08:57 AM.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  8. #8
    template<typename T> threahdead's Avatar
    Join Date
    Sep 2002
    Posts
    214
    Quote Originally Posted by brewbuck View Post
    EDIT: To be more explicit about it, the NULL thing is a non-issue. If T is a pointer type, and an element is NULL, that has nothing to do with returning a reference. You're returning a reference to a pointer, the pointer happens to be NULL, there is no problem here. NULL has nothing to do with anything as far as this decision goes.
    I totally confused a NULL pointer with a value of 0 inside the array when i posted this.

  9. #9
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    If you mean the 'NULL' constant, its value is 0.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reinterpret_cast, C-style cast or function-style cast
    By blight2c in forum C++ Programming
    Replies: 3
    Last Post: 05-14-2002, 10:07 PM
  2. bad style?
    By char in forum C Programming
    Replies: 2
    Last Post: 04-28-2002, 04:32 PM
  3. c-style string vs. c++-style strings
    By Mbrio in forum C++ Programming
    Replies: 3
    Last Post: 02-10-2002, 12:26 PM
  4. how do I extract a style from a DWORD style
    By zMan in forum Windows Programming
    Replies: 1
    Last Post: 01-17-2002, 10:09 AM
  5. Low-Level and High-level ::: C-style And C++ Style
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 12-22-2001, 05:05 PM