开发者

Standard Constructor with CPoint argument in MFC

开发者 https://www.devze.com 2023-01-04 20:22 出处:网络
How can I declare a standard constructor in MFC that expects a CPoint argument, e.g. class CObj { public:

How can I declare a standard constructor in MFC that expects a CPoint argument, e.g.

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

I tried

开发者_运维问答
CObj(CPoint pt = (10,10), float x = 10.0f, int n = 10);

which compiled just fine, but only pt.x got the value 10 while pt.y became 0.

Thanks, RS


I believe something like this should work:

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

Edit: It sure seems to work for me:

#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;
}

Result:

x.x = 10        x.y = 10
0

精彩评论

暂无评论...
验证码 换一张
取 消