問題描述
我有一個嵌套結構,我想要一個指向嵌套成員之一的成員指針:
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/屬性系統.對于每個指定的類,我們保留一個屬性"數組,包括一個 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).
請注意,在語言中實現該功能所需的一切都已經存在.指向成員數據成員的指針與指向直接數據成員的指針沒有任何區別.唯一缺少的是初始化此類指針的語法.然而,委員會顯然對引入這樣的語法不感興趣.
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
指針到數據成員的典型實現只不過是成員從封閉對象開頭的字節偏移量.由于所有成員(立即數和成員的成員)最終在內存中按順序排列,因此成員的成員也可以通過特定的偏移值來標識.當我說這個功能的內部工作已經完全實現時,這就是我的意思,所需要的只是初始化語法.
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.
這篇關于是指向-"內部結構"會員被禁止?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!