如何对CPoint点的XY坐标赋double类型的值?
CPoint point;
point.x=3.4;
我查了MSDN XY坐标的值是int型的,如果我进行上述赋值,实质上X坐标的值是3
如何才能不使数据丢失?
[解决办法]
#ifndef My_DPoint_h__
#define My_DPoint_h__
#include <math.h>
class CDPoint
{
public:
//! The default constructor
/*!
The default constructor does NOT initialize the vector. This is for efficiency
*/
CDPoint(
) {
}
//! Constructor to initialize a vector with (x, y).
CDPoint (
float x,
float y
) {
data[0] = x;
data[1] = y;
}
//! Return a vector whose elements are all zero.
static CDPoint Zero (
) {
CDPoint result;
result.data[0] = 0;
result.data[1] = 0;
return result;
}
//! Return a vector whose elements are all the same and equal to the parameter.
static CDPoint Fill(
float x
) {
CDPoint result;
result.data[0] = x;
result.data[1] = x;
return result;
}
//! Return the X element of the vector
float X(
) const {
return data[0];
}
//! Return the Y element of the vector
float Y(
) const {
return data[1];
}
//! Return an element of the vector specified by the index
/*!
Return an element of (*this) specified by index. Elements are indexed [0..1].
It is a programming error to specify an index out of range.
*/
float Elem(
int index
) const {
return data[index];
}
//! Set an element of the vector specified by the index
/*!
Set an element of (*this) specified by index. The other two elements are unaffected.
Elements are indexed [0..1].
It is a programming error to specify an index out of range.
*/
void SetElem(
int index,
float value
) {
data[index] = value;
}
//! Test the equality of this vector and another vector
/*!
Return true iff (*this) is equal to the other vector according to the built in C/C++
floating point equality operator.
*/
bool Equals(
const CDPoint& other
) const {
return (
data[0] == other.data[0] &&
data[1] == other.data[1]
);
}
//! Test the equality of this vector and the zero vector
/*!
Return true iff (*this) is equal to the zero vector according
to the built in C/C++ floating point equality operator.
*/
bool IsZero(
) const {
return (
data[0] == 0 &&
data[1] == 0
);
}
private:
float data[2];
};
#endif
[解决办法]
#include <windows.h>
class CMyPoint
{
public:
CMyPoint();
CMyPoint(const CMyPoint& Point);
~CMyPoint();
CMyPoint& operator=(const CMyPoint& Point);
void operator+(const CMyPoint& Point1);
protected:
private:
double x;
double y;
};
CMyPoint::CMyPoint()
{
}
CMyPoint::~CMyPoint()
{
}
CMyPoint::CMyPoint(const CMyPoint& Point)
{
this-> x=Point.x;
this-> y=Point.y;
}
CMyPoint& CMyPoint::operator=(const CMyPoint& Point)
{
this-> x=Point.x;
this-> y=Point.y;
return *this;
}
void CMyPoint::operator+(const CMyPoint& Point1)
{
x=x+Point1.x;
y=y+Point1.y;
}