代码之家  ›  专栏  ›  技术社区  ›  We love stackoverflow

参照返回的意义是什么?

c++
  •  16
  • We love stackoverflow  · 技术社区  · 14 年前

    在C++中,

    function() = 10;
    

    如果函数通过引用返回变量,则该函数将起作用。

    11 回复  |  直到 14 年前
        1
  •  20
  •   anon anon    14 年前

    最常见的情况是实现操作符[]之类的东西。

    struct A {
        int data[10];
        int & operator[]( int i ) {
             return data[i];
        }
    };
    

    另一种是通过accesor函数从类返回一个大对象:

    struct b {
        SomeBigThing big;
        const SomeBigThing & MyBig() const {
             return big;
        }
    };
    

        2
  •  7
  •   We love stackoverflow    14 年前

    考虑下面的代码,MyFunction返回一个指向int的指针,并为int设置一个值。

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

    现在缩短到

    *(MyFunction()) = 10;
    

    它的作用与第一个代码块完全相同。

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

    MyFunction() = 10;
    

    这就是我要找的

        3
  •  4
  •   Alexandre C.    14 年前

    例如getter/setter

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

    但在这里,您应该使用一些参数作为公共int。容器提供通过引用返回的函数,例如。 vector<T>::operator[] 这样你就可以写作了 v[k] = x .

        4
  •  3
  •   Naveen    14 年前

    一个非常正常的用例是当您编写一个类似数组的类时。在这里,你想超载 operator [] 所以尽你所能 a[0] = 10; 如果那样的话,你会希望签名像 int& operator[](int index);

        5
  •  2
  •   ereOn    14 年前

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

    允许您编写以下内容:

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

    m_s 而不是返回引用。

        6
  •  2
  •   Philipp    14 年前

    所以把我的答案搞砸了

    struct C { };
    
    C f() {
      return C();
    }
    
    int main() {
      C a;
      f() = a;  // compiles fine
    }
    

    因为这种行为非常令人惊讶,所以通常应该返回常量值或常量引用,除非用户有修改结果的合理意图。

        7
  •  2
  •   mip    14 年前

    它在实现访问器时非常有用

    class Matrix
    {
       public:
          //I skip constructor, destructor etc
    
          int & operator ()(int row, int col)
          {
             return m_arr[row + col * size];
          }
    
       private:
          int size;
          int * m_arr;
    }
    
    Matrix m(10);
    m(1,0) = 10;  //assign a value to row 1, col 0
    
        8
  •  1
  •   MSalters    14 年前

    另一个经典案例:

    class Foo {
      Foo();
    public:
      static Foo& getSingleton();
    };
    
        9
  •  0
  •   ezpz    14 年前

    std::vector operator[] vec[n] = m 否则。

        10
  •  0
  •   DMA57361    14 年前

    您还可以使用returnbyreference实现方法链接(如果您愿意的话)。

    class A
    {
    public:
        A& method1()
        {
            //do something
            return *this;   //return ref to the current object
        }
        A& method2(int i);
        A& method3(float f);  //other bodies omitted for brevity
    };
    
    int main()
    {
        A aObj;
        aObj.method1().method2(5).method3(0.75);
    
        //or use it like this, if you prefer
        aObj.method1()
            .method2(5)
            .method3(0.75);
    }
    
        11
  •  0
  •   Sam Miller    14 年前

    这个 named parameter idiom 是另一个用例。考虑

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

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

    不看页眉就不明显了。与这样的creator类相比

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

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

    假设 Foo 有一个构造函数 const CreateFoo& .