By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,408 Members | 1,796 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,408 IT Pros & Developers. It's quick & easy.

returning refernce of array

P: n/a
I need to know the syntax for writing a reference of an array. I haven't
seen it done often. I have a class with a member array and I want a member
function to return an reference to it. Returning a pointer to the first
element might do but I want to do what I've said.

Fraser.
Jul 23 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
to answer your question:

typedef int array_type[10];
array_type& get_array();

or, to combine it in one:

int (&get_array())[10];

the first definitely looks cleaner.

NOTE, HOWEVER, that you're completely VIOLATING the rules of
encapsulation. a class should never expose it's privates by returning a
reference to them! you might as well make the variable public.
returning a reference to const is slightly better, but not much.

returning an int* is slightly better, but again, not much. you're still
exposing that the class is using an array. what if that changes to a
std::vector, std::list, etc? (don't say "it won't." it may very well
not this time, but if you say "it won't" enough, you'll end up being
wrong sometimes.) what if the array size changes?

understand your design! appearance of getters and setters in a class
indicates that you don't know what member functions, behavior, etc,
that class _should_ have. A class should rarely have getters, far
fewer of them should have setters, and almost none of them should
return a reference to internal data!

at the very least, write functions to return iterators to the beginning
and the end of the array. take that little bit of extra effort now to
encapsulate your data, and maintaining your program will be so much
easier.

perhaps you do have a completely valid reason for returning a
reference. I don't know. I still need to rant about not doing it in
case you don't have a valid reason.

Jul 23 '05 #2

P: n/a
> typedef int array_type[10];
array_type& get_array();
Isn't this returning a reference to a pointer to the first element? I don't
think what I want can be done. I probably confused myself with something
I've seen about template arguments.


or, to combine it in one:

int (&get_array())[10];

the first definitely looks cleaner.
This looks like a pointer to a function only its a reference. You must be
trying to do something that can't be done. This is a declaration of an
array of 10 references of functions that have no parameters and return int.


NOTE, HOWEVER, that you're completely VIOLATING the rules of
encapsulation. a class should never expose it's privates by returning a
reference to them! you might as well make the variable public.
returning a reference to const is slightly better, but not much.

returning an int* is slightly better, but again, not much. you're still
exposing that the class is using an array. what if that changes to a
std::vector, std::list, etc? (don't say "it won't." it may very well
not this time, but if you say "it won't" enough, you'll end up being
wrong sometimes.) what if the array size changes?

understand your design! appearance of getters and setters in a class
indicates that you don't know what member functions, behavior, etc,
that class _should_ have. A class should rarely have getters, far
fewer of them should have setters, and almost none of them should
return a reference to internal data!

at the very least, write functions to return iterators to the beginning
and the end of the array. take that little bit of extra effort now to
encapsulate your data, and maintaining your program will be so much
easier.

perhaps you do have a completely valid reason for returning a
reference. I don't know. I still need to rant about not doing it in
case you don't have a valid reason.


The data in my program is being reorganised and isn't finished yet. I
wasn't thinking that I had the best designs yet.

Fraser.
Jul 23 '05 #3

P: n/a
As always, if you find handling raw array to frustrating, use a class such
as std::vector

ben
Jul 23 '05 #4

P: n/a
"benben"
As always, if you find handling raw array to frustrating, use a class such
as std::vector

ben


I wanted to know something about arrays. I wasn't looking for design
advice.

Fraser.
Jul 23 '05 #5

P: n/a
> I wanted to know something about arrays. I wasn't looking for design
advice.

Fraser.


Fair enough.

An array is nothing more than a pointer to the first element so it is by
itself a reference. Rarely a "reference to an array" is used.

Happy learning!

Regards,
Ben
Jul 23 '05 #6

P: n/a

"benben"
I wanted to know something about arrays. I wasn't looking for design
advice.

Fraser.


Fair enough.

An array is nothing more than a pointer to the first element so it is by
itself a reference. Rarely a "reference to an array" is used.

Happy learning!

Regards,
Ben

Do you have to speak to me like I'm a beginner when I'm certainly not? A
lot of C++ programmers that don't come from C find they don't have a
thorough knowledge about arrays and pointers. If you use containers and
string classes you don't realise you haven't mastered the use of arrays and
related things like pointer conversions. Arrays are used as non-type
template arguments. I'd have to look at that in a book if I was using it
since thats quite a new area of the language to me.

Fraser.
Jul 23 '05 #7

P: n/a
> Do you have to speak to me like I'm a beginner when I'm certainly not? A
lot of C++ programmers that don't come from C find they don't have a
thorough knowledge about arrays and pointers. If you use containers and
string classes you don't realise you haven't mastered the use of arrays and related things like pointer conversions. Arrays are used as non-type
template arguments. I'd have to look at that in a book if I was using it
since thats quite a new area of the language to me.

Fraser.


Sorry if I have offended you. I really was just trying to be helpful...And I
really wasn't trying to speak like you are a beginner, a lot of people don't
use raw arrays just like you said, and that's what's encouraged. That's
that.

ben
Jul 23 '05 #8

P: n/a
Fraser Ross wrote:
typedef int array_type[10];
array_type& get_array();
Isn't this returning a reference to a pointer to the first element?


No. Square brackets indicate arrays, and stars indicate pointers.
(Except within the formal parameter list of a function.)
This function returns a reference to an array of 10 ints.
I don't think what I want can be done.
It can be done, and the GP said how to do it.
I probably confused myself with something I've seen about
template arguments.


You can use references to arrays in templates, just like any type:

template<typename T, int N>
size_t lengthof(T (&t)[N]) { return N; }
or, to combine it in one:

int (&get_array())[10];

the first definitely looks cleaner.


This looks like a pointer to a function only its a reference.
You must be trying to do something that can't be done. This
is a declaration of an array of 10 references of functions
that have no parameters and return int.


I guess "something that can't be done" refers to you learning
how to read declarations. The above statement declares a function
called 'get_array' which takes no parameters, and returns a reference
to array of 10 int.

#include <iostream>

int array[10];

int (&get_array())[10]
{
return array;
}

int main()
{
array[1] = 5;
std::cout << get_array()[1] << std::endl;
}

Jul 23 '05 #9

P: n/a
Me
Fraser Ross wrote:
typedef int array_type[10];
array_type& get_array();
Isn't this returning a reference to a pointer to the first element?


No. It's returning a reference to a 10 element int array. If it was a
reference to a pointer, it would look like:

typedef int *array_type;
array_type& get_array();
int (&get_array())[10];

the first definitely looks cleaner.


This looks like a pointer to a function only its a reference.


get_array is a function that returns a reference to an array of 10
ints. What you describe (get_array is a reference to a function that
returns an array of 10 ints), which is illegal, but this is what it
would look like if it were allowed:

int (&get_array)()[10];
You must be
trying to do something that can't be done. This is a declaration of an
array of 10 references of functions that have no parameters and return int.


You can't create an array of references either. But if you could, it
would look like:

int (&get_array[10])();

get_array is an array of 10 references to functions that take nothing
and return an int.

I think you should familiarize yourself with:

http://www.ericgiguere.com/articles/...larations.html

In most places you can just swap the * in "pointer to" with the & in
"reference to" declarations and just keep track of the places that
references aren't allowed (i.e. reference to reference, array of
reference, pointer to reference, etc).

Jul 23 '05 #10

P: n/a
benben wrote:
Do you have to speak to me like I'm a beginner when I'm certainly not? A
lot of C++ programmers that don't come from C find they don't have a
thorough knowledge about arrays and pointers. If you use containers and
string classes you don't realise you haven't mastered the use of arrays


and
related things like pointer conversions. Arrays are used as non-type
template arguments. I'd have to look at that in a book if I was using it
since thats quite a new area of the language to me.

Fraser.

Sorry if I have offended you. I really was just trying to be helpful...And I
really wasn't trying to speak like you are a beginner, a lot of people don't
use raw arrays just like you said, and that's what's encouraged. That's
that.


You certain were not out of line. The OP needs to not take offence, this
happens frequently, one never knows how much the poster actual knows and
so being verbose does no harm.
Jul 23 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.