tags:

views:

11

answers:

1

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

A: 

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
Jerry Coffin
No, it doesn't. Same result as with my approach: pt.x is 10, but pt.y is 0
chessweb