联盟里面的类访问
问题描述:
我有第三方库里的类A
的声明,所以我不能修改它。 我需要使用类B
的声明将它传递给方法,有没有办法做到这一点,而无需修改类A
?联盟里面的类访问
当我试试这个:
#include <iostream>
using namespace std;
class A
{
public:
union {
class B
{
public:
int x;
};
}un;
};
void foo(A::B & test)
{
}
int main() {
A::B test;
test.x=10;
cout << test.x << endl;
return 0;
}
我得到的错误:
error:
B
is not a member ofA
我的假设是,这是因为B
是一个未命名的命名空间。
PS:
union {...
到:如果我可以从修改的union
声明
A::T::B test;
答
您可以:
union T {...
这将通过完成简单使用decltype
获得工会的类型,那么你可以访问B
:
decltype(std::declval<A&>().un)::B test;
哇!那很快!谢谢,btw我不知道这是否也可以为c + + 03做 – Rama