2010-06-28 42 views
0

如何在MFC中聲明一個標準構造函數,該構造函數需要CPoint參數,例如在MFC中具有CPoint參數的標準構造函數

class CObj { 
public: 
    CObj(CPoint pt = ???, float x = 10.0f, int n = 10); 
    ... 

我試圖

CObj(CPoint pt = (10,10), float x = 10.0f, int n = 10); 

它編譯就好了,但只得到了pt.x值10,同時pt.y成了0

感謝,RS

回答

0

我相信像這樣的東西應該工作:

CObj(Cpoint pt = CPoint(10,10), float x = 10.0f, int n = 10); 

編輯:這肯定似乎爲我工作:

#include <iostream> 

struct CPoint { 
    int x, y; 
    CPoint(int x_, int y_) : x(x_), y(y_) {} 
}; 

class CObj { 
    CPoint p; 
public: 
    CObj(CPoint pt = CPoint(10,10), float x = 10.0f, int n = 10) : p(pt) { 
     std::cout << "x.x = " << p.x << "\tx.y = " << p.y << std::endl; 
    } 
}; 

int main() { 
    CObj x; 
    return 0; 
} 

結果:

x.x = 10  x.y = 10 
+0

不,不。與我的方法相同的結果:pt.x是10,但pt.y是0 – chessweb 2010-06-29 21:34:38

相關問題