是指针到“内部结构“成员禁止? [英] Is Pointer-to- " inner struct" member forbidden?

查看:122
本文介绍了是指针到“内部结构“成员禁止?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个嵌套结构,我想有一个嵌套成员的指针:

i have a nested struct and i'd like to have a pointer-to-member to one of the nested member:

是合法的吗?

struct InnerStruct
{
    bool c;
};
struct MyStruct {
    bool t;
    bool b;
    InnerStruct inner;
};

这:

MyStruct mystruct;
//...
bool MyStruct::* toto = &MyStruct::b;

可以,但:

bool MyStruct::* toto = &MyStruct::inner.c;

不是。任何想法?

感谢

以下是一些细节
是的是& MyStruct :: b而不是mystruct :: b;
代码来自自定义RTTI / Property系统。
对于每个指定的类,我们保存一个Property数组,包括一个Ptr-to-member
这样使用:

Here are some details Yes it is &MyStruct::b and not mystruct::b; The code is from a custom RTTI/Property system. For each specified class we keep an array of "Property", including a Ptr-to-member It is used like this:

//somewhere else in code...
( myBaseClassWithCustomRTTIPointer)->* toto = true;


推荐答案

是的,你不是第一个提出这个完全合乎逻辑的想法。在我看来,这是在C ++中的指针指向成员的明确的错误/省略之一,但显然委员会没有兴趣开发指针的成员的规范任何进一步(因为大多数低级语言特性的情况)。

Yes, it is forbidden. You are not the first to come up with this perfectly logical idea. In my opinion this is one of the obvious "bugs"/"omissions" in the specification of pointers-to-members in C++, but apparently the committee has no interest in developing the specification of pointers-to-members any further (as is the case with most of the "low-level" language features).

请注意,在语言中已经存在的所有必要的功能。指向a-data-member-of-a-member的指针与指向立即数据成员的指针没有什么不同。唯一缺少的是初始化这样的指针的语法。但是,委员会显然不想引入这样的语法。

Note that everything necessary to implement the feature in already there, in the language. A pointer to a-data-member-of-a-member is in no way different from a pointer to an immediate data member. The only thing that's missing is the syntax to initialize such a pointer. However, the committee is apparently not interested in introducing such a syntax.

从纯形式逻辑的角度来看,这应该是在C ++

From the pure formal logic point of view, this should have been allowed in C++

struct Inner {
  int i;
  int j[10];
};

struct Outer {
  int i;
  int j[10];
  Inner inner;
};

Outer o;
int Outer::*p;

p = &Outer::i; // OK
o.*p = 0; // sets `o.i` to 0

p = &Outer::inner.i; // ERROR, but should have been supported
o.*p = 0; // sets `o.inner.i` to 0

p = &Outer::j[0]; // ERROR, but should have been supported
o.*p = 0; // sets `o.j[0]` to 0
// This could have been used to implement something akin to "array type decay" 
// for member pointers

p = &Outer::j[3]; // ERROR, but should have been supported
o.*p = 0; // sets `o.j[3]` to 0

p = &Outer::inner.j[5]; // ERROR, but should have been supported
o.*p = 0; // sets `o.inner.j[5]` to 0

to-data-member只不过是成员从包围对象的开始的字节偏移。由于所有成员(成员的直接成员和成员)最终被顺序地布置在存储器中,成员的成员也可以通过特定偏移值来标识。这是我的意思,当我说这个功能的内部工作已经完全实现,所需要的是初始化语法。

A typical implementation of pointer-to-data-member is nothing more than just an byte-offset of the member from the beginning of the enclosing object. Since all members (immediate and members of members) are ultimately laid out sequentially in memory, members of members can also be identified by a specific offset value. This is what I mean when I say that the inner workings of this feature are already fully implemented, all that is needed is the initialization syntax.

在C语言中,此功能是通过标准 offsetof 宏获得的显式偏移进行仿真。在C中我可以获得 offsetof(Outer,inner.i) offsetof(Outer,j [2])。不幸的是,这种能力没有反映在C ++指针到数据成员中。

In C language this functionality is emulated by explicit offsets obtained through the standard offsetof macro. And in C I can obtain offsetof(Outer, inner.i) and offsetof(Outer, j[2]). Unfortunately, this capability is not reflected in C++ pointers-to-data-members.

这篇关于是指针到“内部结构“成员禁止?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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