C++ function returning reference to array

后端 未结 5 1296
天命终不由人
天命终不由人 2021-01-04 08:47

Is there any other way to receive a reference to an array from function returning except using a pointer?

Here is my code.

int ia[] = {1, 2, 3};
decl         


        
5条回答
  •  春和景丽
    2021-01-04 09:03

    Is there any other way to receive a reference to an array from function returning except using a pointer?

    Yes, using a reference to an array, like with any other type:

    int (&ref)[3] = a.foo();
    

    To avoid the clunky syntax, you could use a typedef instead.

    typedef int int_array3[3];
    
    ...
    int_array3& foo() { return ia; }
    
    ...
    
    int_array3& ref = a.foo();
    

提交回复
热议问题