Z
zaeminkr
I got a good answer here I have still confusing part.
I have two very simple classes
class DRect
{
private :
double x0, y0, x1, y1;
public :
DRect(double a, double b, double c, double d) : x0(a), y0(b),
x1(c), y1(d) {}
void Union(const DRect* p)
{
x0 = MIN(x0, p->x0);
y0 = MIN(y0, p->y0);
x1 = MAX(x1, p->x1);
y1 = MAX(y1, p->y1);
}
};
class IRect
{
private :
int x0, y0, x1, y1;
public :
IRect(int a, int b, int c, int d) : x0(a), y0(b), x1(c), y1(d)
{}
operator DRect() const
{
return DRect(x0, y0, x1, y1);
}
operator DRect*() const
{
return &DRect(x0, y0, x1, y1);
}
};
and the execution code here.
void main()
{
DRect d(5.3, 5.3, 15.6, 15.6);
IRect i(10, 10, 100, 100);
/* 1. No problem with compiling this code. */
d.Union(&(static_cast<DRect>(i)));
/* 2. It isn't compiled. */
d.Union(&i);
}
My question is
1. The second execution code 'd.Union(&i)' is quite resonable. People
usually do something like this.
DRect d1(2.3, 5.3, 98.2, 34.2);
DRect d2(5.2, 4.2, 99.1, 54.9);
d1.Union(&d2);
So I want people can do something like this also.
DRect d1(2.3, 5.3, 98.2, 34.2);
IRect i(5, 4, 99, 54);
d1.Union(&i);
However, the compiler is only accept
'd.Union(&(static_cast<DRect>(i)));' form. My 'operator' functions are
wrong?
2. Where is the temporary object exist? stack?
operator DRect() const
{
return DRect(x0, y0, x1, y1);
}
operator DRect*() const
{
return &DRect(x0, y0, x1, y1);
}
If the temporary DRect object is exist on stack, it's still exist
when the 'DRect::Union' is executed?
Thanks.
I have two very simple classes
class DRect
{
private :
double x0, y0, x1, y1;
public :
DRect(double a, double b, double c, double d) : x0(a), y0(b),
x1(c), y1(d) {}
void Union(const DRect* p)
{
x0 = MIN(x0, p->x0);
y0 = MIN(y0, p->y0);
x1 = MAX(x1, p->x1);
y1 = MAX(y1, p->y1);
}
};
class IRect
{
private :
int x0, y0, x1, y1;
public :
IRect(int a, int b, int c, int d) : x0(a), y0(b), x1(c), y1(d)
{}
operator DRect() const
{
return DRect(x0, y0, x1, y1);
}
operator DRect*() const
{
return &DRect(x0, y0, x1, y1);
}
};
and the execution code here.
void main()
{
DRect d(5.3, 5.3, 15.6, 15.6);
IRect i(10, 10, 100, 100);
/* 1. No problem with compiling this code. */
d.Union(&(static_cast<DRect>(i)));
/* 2. It isn't compiled. */
d.Union(&i);
}
My question is
1. The second execution code 'd.Union(&i)' is quite resonable. People
usually do something like this.
DRect d1(2.3, 5.3, 98.2, 34.2);
DRect d2(5.2, 4.2, 99.1, 54.9);
d1.Union(&d2);
So I want people can do something like this also.
DRect d1(2.3, 5.3, 98.2, 34.2);
IRect i(5, 4, 99, 54);
d1.Union(&i);
However, the compiler is only accept
'd.Union(&(static_cast<DRect>(i)));' form. My 'operator' functions are
wrong?
2. Where is the temporary object exist? stack?
operator DRect() const
{
return DRect(x0, y0, x1, y1);
}
operator DRect*() const
{
return &DRect(x0, y0, x1, y1);
}
If the temporary DRect object is exist on stack, it's still exist
when the 'DRect::Union' is executed?
Thanks.