Whats the significance of return by reference?

后端 未结 11 1226
既然无缘
既然无缘 2021-02-13 00:15

In C++,

function() = 10;

works if the function returns a variable by reference.

What are the use cases of it?

相关标签:
11条回答
  • 2021-02-13 00:55

    Another classic case:

    class Foo {
      Foo();
    public:
      static Foo& getSingleton();
    };
    
    0 讨论(0)
  • 2021-02-13 01:00

    Getters/setters for instance

    class C
    {
        int some_param_;
    public:
        int& param() { return some_param_; }
        int const& param() const { return some_param_; }
    };
    

    but here you should go with some_param being a public int. Containers provide functions that return by reference, eg. vector<T>::operator[] so that you can write v[k] = x.

    0 讨论(0)
  • 2021-02-13 01:00

    The named parameter idiom is a another use case. Consider

    class Foo
    {
    public:
        Foo(
            int lions,
            float tigers,
            double bears,
            std::string zookeeper
        );
    };
    

    users of this class need to remember the position of each parameter

    Foo foo( 1, 2.0, 5, "Fred" );
    

    which can be non-obvious without looking at the header. Compared to a creator class like so

    class CreateFoo
    {
    friend class Foo;
    public:
        CreateFoo();
    
        CreateFoo& lions(int lions) {
            _lions = lions;
             return *this;
        }
    
        CreateFoo& tigers(float tigers) {
            _tigers = tigers;
            return *this;
        }
    
        CreateFoo& bears(double bears) {
            _bears = bears;
            return *this;
        }
    
        CreateFoo& zookeeper(const std::string& zookeeper) {
            _zookeeper = zookeeper;
            return *this;
        }
    
    private:
        int _lions;
        float _tigers;
        double _bears;
        std::string _zookeeper;
    };
    

    which can then be used by clients like so

    Foo foo = CreateFoo().
        lions(1).
        tigers(2.0).
        zookeeper("Fred").
        bears(5)
        ;
    

    assuming Foo has a constructor taking a const CreateFoo&.

    0 讨论(0)
  • 2021-02-13 01:05

    A very normal use case is when you write an array like class. Here you want to overload the operator [] so as you can do a[0] = 10; In that case you would want the signature to be like int& operator[](int index);

    0 讨论(0)
  • 2021-02-13 01:05

    In case you have a class that contains another structure, it can be useful to directly modify the contained structure:

    struct S
    {
        int value;
    };
    
    class C
    {
        public:
    
            S& ref() { return m_s; }
    
        private:
    
            S m_s;
    };
    

    Allows you to write something like:

    void foo()
    {
        C c;
    
        // Now you can do that:
    
        c.ref().value = 1;
    }
    

    Note: in this example it might be more straightforward to directly make m_s public rather than returning a reference.

    0 讨论(0)
  • 2021-02-13 01:07

    Consider the following code, MyFunction returns a pointer to an int, and you set a value to the int.

    int  *i;
    i = MyFunction();
    *i = 10;
    

    Now shorten that to

    *(MyFunction()) = 10;
    

    It does exactly the same thing as the first code block.

    You can look at a reference as just a pointer that's always dereferenced. So if my function returned a reference - not a pointer - to an int the frist code block would become

    int  &i;
    i = MyFunction();
    i = 10;
    

    and the second would become

    MyFunction() = 10;
    

    This is what i was looking for

    0 讨论(0)
提交回复
热议问题