473,836 Members | 1,484 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

returning refernce of array

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
10 10302
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
> 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
As always, if you find handling raw array to frustrating, use a class such
as std::vector

ben
Jul 23 '05 #4
"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
> 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

"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
> 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
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<typena me 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
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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
14041
by: Krackers | last post by:
How do you write a function which returns a reference to an array. I can only get a function to return a copy of the array itself. I've had a look at some other threads in this group an the return value of a function acts like 'by Val' returning the value only (except for objects) can you make it return a reference instead? cheers, Krackers
5
1923
by: Robert | last post by:
Hi, This might be a strange question but i would like to know how to return an array from a function. Do you have to use pointers for this? Thanks in advance, Robert
2
2325
by: Interop newbie | last post by:
Hello, The following code makes an interop call to a Com object from C#: //Create a Bom object NewsBusObjsRCW.NewsBomObjClass myNewsBom = new NewsBusObjsRCW.NewsBomObjClass();
3
2706
by: Faustino Dina | last post by:
Hi, The following code is from an article published in Informit.com at http://www.informit.com/guides/content.asp?g=dotnet&seqNum=142. The problem is the author says it is not a good idea to return an array as a property because it will return a copy of the array instead a reference to it. How can I force the property to return a reference to the array? Is it only a feature of arrays? I hope normal class objects (including collections)...
5
19605
by: Stacey Levine | last post by:
I have a webservice that I wanted to return an ArrayList..Well the service compiles and runs when I have the output defined as ArrayList, but the WSDL defines the output as an Object so I was having a problem in the calling program. I searched online and found suggestions that I return an Array instead so I modified my code (below) to return an Array instead of an ArrayList. Now I get the message when I try to run just my webservice...
13
4791
by: Jordan Tiona | last post by:
Is this not allowed in C++? I try in MSVC, but I always get an error, and the function no longer shows up in my class view.
17
11525
by: kleary00 | last post by:
Hi, I am writing a function that needs to return an array of strings and I am having some trouble getting it right. I need some help. Here is what I consider an array of 100 strings: char *array_string Thanks, -Kim
0
1037
by: DR | last post by:
is there any speed difference between a TVF CLR that returns an array vs yield as each result is ready? e.g. does returning the array all at once cause sql server to allocate more efficiently?
5
2683
by: ctj951 | last post by:
I have a very specific question about a language issue that I was hoping to get an answer to. If you allocate a structure that contains an array as a local variable inside a function and return that structure, is this valid? As shown in the code below I am allocating the structure in the function and then returning the structure. I know if the structure contained only simple types (int, float) this will work without problems as you...
0
10860
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10604
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9389
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7796
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6984
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5831
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4466
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4026
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3116
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.