2017-02-23 47 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