在嵌套的匿名结构上忽略C ++私有修饰符 [英] C++ private modifier ignored on nested anonymous struct
问题描述
下面的代码在Visual C ++中编译得很好:
The following sample code compiles just fine in Visual C++:
class Test {
private:
struct {
struct {
int privateData;
};
};
};
int main(int, char **)
{
Test test;
test.privateData = 0;
return 0;
}
但是为什么?我期望一个编译器错误,因为 privateData
成员应该是不可访问的函数main,因为它应该是 private
喜欢它的容器的容器。
我知道无名结构不是官方C ++的一部分,但这个设计是asinine。
But why? I'd expect a compiler error because the privateData
member should be inaccessible to the function main, since it's supposed to be private
like its container's container.
I know that nameless structs are not part of official C++, but this design is asinine.
顺便说一下,我也试图改变 private into
into
into
和
/ code>:看起来编译器拒绝对嵌套在另一个匿名结构或联合体中的匿名结构体和联合体授予访问修饰符。
By the way I've also tried to change private
into protected
and struct
into union
: it looks like the compiler refuses to honor access modifiers on anonymous structs and unions that are nested inside another anonymous struct or union.
这个功能?
推荐答案
是的,这是一个错误。微软承认,反馈报告在这里。
Yes, it is a bug. Microsoft acknowledged it is, the feedback report is here.
现在,错误是在不会修复状态,目前还不清楚什么时候(如果有的话)将被处理。有一个有点奇怪的解决方法,IntelliSense解析器内置到Visual Studio,由爱迪生设计集团,写的,抱怨。您会得到红色的波浪和消息:
Right now the bug is in "will not fix" status and it is unclear when (if ever) it will be addressed. There is a somewhat odd workaround for it, the IntelliSense parser built into Visual Studio, written by the Edison Design Group, does complain about it. You get the red squiggles and the message:
错误:成员Test.privateData(在第10行声明)是无法访问
Error: member "Test.privateData" (declared at line 10) is inaccessible
这篇关于在嵌套的匿名结构上忽略C ++私有修饰符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!