class Base {
protected:
union {
struct {
bool bBold : 1;
bool bFakeBold : 1;
};
int a;
};
public:
bool isBold() {
return bBold;
}
};
Test類:爲什麼保護不會保護這個班級的成員?
#include <assert.h>
#include <stdio.h>
int main()
{
Base d;
d.bBold = false;
assert(d.isBold() == false);
d.bBold = true;
assert(d.isBold() == true);
printf("good");
return 0;
}
兩個msvc11和g ++編譯沒有任何錯誤。
爲什麼?
看起來還有一個不使用工會的理由。 –
clang 3.2給出了'錯誤:'bBold'是預期的'Base'的受保護成員。 gcc 4.7.2不抱怨(對於'protected',甚至對'private') – alfC
msvc給出警告[C4201](http://msdn.microsoft.com/en-us/library/c89bw853(v = vs .71).aspx) – spiritwolfform