这个方法声明/定义是什么意思? (与传递数组有关?)

发布于 2024-10-12 01:32:09 字数 448 浏览 6 评论 0原文

嗨,我在遗留代码中跌跌撞撞,遇到了一个奇怪的方法定义/声明。我对它的作用有一个有根据的猜测,但我还不能 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

坏尐絯 2024-10-19 01:32:09

它是一个 const 成员函数的声明,不带参数并返回对 SOME_CONSTANT_VALUE const SomeEnumeratedId 数组的引用s。

使用 typedef 看起来更容易理解。

typedef const SomeEnumeratedId SomeArrayType[SOME_CONSTANT_VALUE];

SomeArrayType& SomeClass::SomeMethod() const
{
    return someMemberArray;
}

It's the declaration of a const member function taking no parameters and returning a reference to an array of SOME_CONSTANT_VALUE const SomeEnumeratedIds.

It looks easier to understand with a typedef.

typedef const SomeEnumeratedId SomeArrayType[SOME_CONSTANT_VALUE];

SomeArrayType& SomeClass::SomeMethod() const
{
    return someMemberArray;
}
牛↙奶布丁 2024-10-19 01:32:09

正如 @Charles 已经指出的那样,这种奇怪的符号是常量方法的声明/定义,该常量方法返回对存储为成员的元素数组的引用。

语法非常奇怪,可以(并且应该)通过 typedef 来简化:

typedef SomeEnumerated array_t[SOME_CONSTANT_VALUE];
const array_t& SomeMethod() const;

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:

typedef SomeEnumerated array_t[SOME_CONSTANT_VALUE];
const array_t& SomeMethod() const;
清浅ˋ旧时光 2024-10-19 01:32:09

是的,这是 C 完全向后的类型声明语法的结果。这类似于在进行数组 typedef 时的写法:typedef int myArrayType[3];,其中 [3] 在新类型名后面,而不是原始类型名后面。

如果你真的很聪明,你可以使用 {std,tr1,boost}::array ——无论如何值得考虑 ——这样你最终会得到:

 array<SomeEnumeratedId, SOME_CONSTANT_VALUE>& SomeClass::SomeMethod() const;

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:

 array<SomeEnumeratedId, SOME_CONSTANT_VALUE>& SomeClass::SomeMethod() const;

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文