Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ function returning reference to array

Tags:

c++

c++11

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};
decltype(ia) &foo() {   // or, int (&foo())[3]
    return ia;
}

int main() {
    int *ip1 = foo();   // ok, and visit array by ip1[0] or *(ip1 + 0)
    auto ip2 = foo();   // ok, the type of ip2 is int *
    int ar[] = foo();   // error
    int ar[3] = foo();  // error
    return 0;
}

And a class version.

class A {
public:
    A() : ia{1, 2, 3} {}
    int (&foo())[3]{ return ia; }
private:
    int ia[3];
};

int main() {
    A a;
    auto i1 = a.foo();    // ok, type of i1 is int *, and visit array by i1[0]
    int i2[3] = a.foo();  // error
    return 0;
}

Note: const qualifier is omitted in code.

I know the name of the array is a pointer to the first element in that array, so using a pointer to receive is totally viable.

Sorry, I made a mistake. From Array to pointer decay

There is an implicit conversion from lvalues and rvalues of array type to rvalues of pointer type: it constructs a pointer to the first element of an array.

Please ignore that XD

I'm just curious about the question I asked at the beginning :)

like image 662
Jaege Avatar asked Dec 23 '15 15:12

Jaege


People also ask

Which function returns a reference to array of?

'fetchrow_arrayref()' returns a reference to an array of row values.

Can a function return an array in C?

So in simple words, Functions can't return arrays in C.

How do you return an array by reference in C++?

Return Array from Functions in C++ C++ does not allow to return an entire array as an argument to a function. However, you can return a pointer to an array by specifying the array's name without an index.

Can you return by reference in C?

C # in TeluguA C++ function can return a reference in a similar way as it returns a pointer. When returning a reference, be careful that the object being referred to does not go out of scope. So it is not legal to return a reference to local var. But you can always return a reference on a static variable.


2 Answers

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();
like image 111
juanchopanza Avatar answered Sep 29 '22 23:09

juanchopanza


You should use std::array to avoid confusion and make cleaner, safer and less clunky code:

class A {
public:
    typedef std::array<int,3> array;
    A() : ia{1, 2, 3} {}
    array &foo(){ return ia; }
private:
    array ia;
};

int main() {
    A a;
    auto i1 = a.foo();      // ok, type of i1 is A::array, it is a copy and visit array by i1[0]
    for ( int i : i1 ) {}   // you can iterate now, with C array you cannot anymore
    auto &i2 = a.foo();     // ok, type of i2 is A::array&, you can change original by i2[0] = 123 
    A::array i3 = a.foo();  // fine, i3 is now a copy and visit array by i3[0]
    A::array &i4 = a.foo(); // fine, you can change original by i4[0] = 123
    int *i5 = a.foo().data();  // if you want old way to pass to c function for example, it is safer you explicitly show your intention
    return 0;
}

I know the name of the array is a pointer to the first element in that array

This is incorrect, array can be implicitly converted to a pointer to the first element. It is not the same.

like image 43
Slava Avatar answered Sep 30 '22 01:09

Slava