2017-04-14 36 views
1
class M33 
{ 
public: 
    double m[3][3]; 

    double (*GetM())[3] { 
     return m; 
    } 
}; 

// call to JPL cspice f2c generated routine 
// void mxm_c (const double m1 [3][3], 
//    const double m2 [3][3], 
//    double   mout[3][3]) 

void test() 
{ 
    M33 m1; 
    M33 m2; 
    M33 mOut; 
    mxm_c(m1.m, m2.m, mOut.m); // this works 
    mxm_c(m1.GetM(), m2.GetM(), mOut.GetM()); // this works 
} 

使用VS2013。 問:是否可以使用強制轉換運算符,如...鑄造操作員重載返回2-dim陣列以訪問外部庫

operator double*[3]() // this does not compile 
{ 
    return m; 
} 

從而使得這種編碼風格的快捷方式?

mxm_c(m1, m2, mOut); // this does not work 

回答

1

是的,你可以做這樣的操作符。當複雜類型的時候,最好引入一個別名:

using Ptr = double (*)[3]; 

operator Ptr() 
{ 
    return GetM(); 
} 

[Live example]