这个方法声明/定义是什么意思? (与传递数组有关?)
嗨,我在遗留代码中跌跌撞撞,遇到了一个奇怪的方法定义/声明。我对它的作用有一个有根据的猜测,但我还不能 100% 确定。
声明:
const SomeEnumeratedId (&SomeMethod() const)[SOME_CONSTANT_VALUE];
定义
const SomeEnumeratedId (&SomeClass::SomeMethod() const)[SOME_CONSTANT_VALUE]
{
return someMemberArray;
}
我最好的猜测是它正在传递对 someMemberArray 的引用,并且保证它的大小为 SOME_CONSTANT_VALUE,但我从未见过方法声明后面出现的 [] 符号,而且有这么多括号。
非常感谢任何帮助。
Hi I was stumbling through legacy code, and I came across a wierd method definition/declaration. I have an educated guess of what it does, but I cannot be 100% sure yet.
declaration:
const SomeEnumeratedId (&SomeMethod() const)[SOME_CONSTANT_VALUE];
definition
const SomeEnumeratedId (&SomeClass::SomeMethod() const)[SOME_CONSTANT_VALUE]
{
return someMemberArray;
}
My best guess is that it is passing a reference to someMemberArray and that it is guaranteeing that it is of size SOME_CONSTANT_VALUE, but I have never seen the [] notation after the method declaration as it appears, and there are so many parentheses.
Any help greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
它是一个
const
成员函数的声明,不带参数并返回对SOME_CONSTANT_VALUE
const
SomeEnumeratedId
数组的引用s。使用
typedef
看起来更容易理解。It's the declaration of a
const
member function taking no parameters and returning a reference to an array ofSOME_CONSTANT_VALUE
const
SomeEnumeratedId
s.It looks easier to understand with a
typedef
.正如 @Charles 已经指出的那样,这种奇怪的符号是常量方法的声明/定义,该常量方法返回对存储为成员的元素数组的引用。
语法非常奇怪,可以(并且应该)通过 typedef 来简化:
That weird notation, as @Charles has already pointed out is the declaration/definition of a constant method that returns a reference to an array of elements stored as a member.
The syntax is quite bizarre and could (and should probably) be simplified by means of a typedef:
是的,这是 C 完全向后的类型声明语法的结果。这类似于在进行数组 typedef 时的写法:
typedef int myArrayType[3];
,其中[3]
在新类型名后面,而不是原始类型名后面。如果你真的很聪明,你可以使用
{std,tr1,boost}::array
——无论如何值得考虑 ——这样你最终会得到:。
typedef 的解决方法(在其他答案中探讨)是相关的,尽管并不完全等同,因为
{std,tr1,boost}::array
是包装器,而不仅仅是 typedef。Yea, it's a consequence of C's utterly backwards type declaration syntax. It's similar to how, when doing an array typedef, you write this:
typedef int myArrayType[3];
, with the[3]
after the new typename, not the original.If you're really clever you can use
{std,tr1,boost}::array
-- worth considering anyway -- so that you end up with:instead.
The workarounds with typedefs (explored in other answers) are related, although not quite equivalent as
{std,tr1,boost}::array
are wrappers, not just typedefs.