2017-02-23 49 views
0

我有第三方库里的类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 of A

Live Example!

我的假设是,这是因为B是一个未命名的命名空间。

PS:

union {... 

到:如果我可以从修改的union 声明

A::T::B test; 

回答

1

您可以:

union T {... 

这将通过完成简单使用decltype获得工会的类型,那么你可以访问B

decltype(std::declval<A&>().un)::B test; 

coliru example

+0

哇!那很快!谢谢,btw我不知道这是否也可以为c + + 03做 – Rama