返回引用数组 [英] returning refernce of array

查看:65
本文介绍了返回引用数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要知道编写数组引用的语法。我没有经常看到这么做的事情。我有一个带有成员数组的类,我想要一个成员

函数来返回对它的引用。返回指向第一个

元素的指针可能会有,但我想做我说过的话。


Fraser。

解决方案

回答你的问题:


typedef int array_type [10];

array_type& get_array();


或者,将它合并为一个:


int(& get_array())[10];


第一个肯定看起来更干净。


注意,但是,你完全违反了规则

封装。一个班级不应该通过向他们返回

引用来揭露它的私有性!你也可以将变量公之于众。

返回对const的引用稍微好一些,但不多。


返回int *稍微好一点,但同样,并不多。你还在露出该类正在使用数组的
。如果改为

std :: vector,std :: list等怎么办? (不要说它不会。它可能很好

不是这次,但如果你说它不够,你会最终有时会因为b $ b错误而导致错误。)如果数组大小发生变化怎么办?


了解你的设计!一个类中的getter和setter的出现

表示你​​不知道成员函数,行为等等,这个类_should_有
。一个班级应该很少有吸气剂,远远不是b $ b他们应该有更少的安装者,而且几乎没有人应该

返回对内部数据的引用!


至少,编写函数将迭代器返回到开头

和数组的结尾。现在花一点点额外的努力来

封装你的数据,维护你的程序将会非常多。

更容易。


也许你确实有一个完全有效的理由返回

参考。我不知道。如果你没有正当理由,我仍然需要咆哮不要这样做。


> typedef int array_type [10];

array_type& get_array();


这不是返回对第一个元素指针的引用吗?我不认为我想要的可以做到。我可能会把自己弄糊涂的东西

我已经看过模板参数。


或者将它合二为一:
int(& get_array())[10];

第一个肯定看起来更干净。


这看起来像是指向函数的指针而只是它的引用。你必须要b / b
尝试做一些无法做到的事情。这是一个包含10个函数引用的

数组的声明,这些函数没有参数并返回int。


注意,但是,你完全是违反
封装的规则。一个班级不应该通过返回对他们的引用来揭露它的私有性!你可以把变量公之于众。
返回对const的引用稍微好一些,但不多。

返回int *稍微好一些,但是再次,并不多。你还在揭露这个类正在使用一个数组。如果改为
std :: vector,std :: list等怎么办? (不要说它不会。它可能很好
不是这一次,但如果你说它不够,你最终会<有时会出错。)如果阵列大小发生变化怎么办?

了解你的设计!一个类中的getter和setter的出现
表示你不知道该类_should_具有哪些成员函数,行为等。一个班级应该很少有吸气剂,远远没有多少人应该有安装者,而且他们几乎都不应该返回对内部数据的引用!

至少,写函数将迭代器返回到开头
和数组的结尾。现在花一些额外的努力来封装你的数据,维护你的程序将会更加容易。

也许你确实有一个完全有效的理由返回一个
参考。我不知道。如果你没有正当理由,我仍然需要咆哮不要这样做。




我的程序中的数据正在重新组织,还没完呢我没想到我有最好的设计。


弗雷泽。


与往常一样,如果你发现处理原始数组令人沮丧,请使用类似

作为std :: vector


ben

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.

解决方案

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.


> 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.


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

ben


这篇关于返回引用数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆