저는 자체 Windows API 래퍼를 시작하고 C++ 기능을 포함하도록 구조를 다시 작성하는 동안 익숙하지 않은 주제로 실행했습니다.어디서나 데이터 멤버에 액세스 할 수 있지만 읽기 전용
나는이를 돌리겠다 :
typedef struct _RECT {
LONG left;
LONG top;
LONG right;
LONG bottom;
} RECT, *PRECT;
#define RECT_POS 1
#define RECT_SIZE 2
typedef struct WrapperRect // RECT
{
WrapperRect(); // all 0
WrapperRect (const double, const double, const double, const double, bool = RECT_POS); // initalize with tl pos and either br pos or size
bool set (const double, const double, const double, const double, bool = RECT_POS); // set tl pos and either br pos or size
bool pos (const double, const double); // set tl pos
bool size (const double, const double); // set size
WrapperRect & operator= (const WrapperRect &); // assign another rect
bool operator== (const WrapperRect &); // check for equality (pos+size)
bool operator!= (const WrapperRect &); // check for inequality (pos+size)
bool operator> (const WrapperRect &); // check for tl pos greater
bool operator< (const WrapperRect &); // check for tl pos less
bool operator>= (const WrapperRect &); // check for tl pos greater equal
bool operator<= (const WrapperRect &); // check for tl pos less equal
WrapperRect & operator+ (const POINT &); // move down/right
WrapperRect & operator- (const POINT &); // move up/left
WrapperRect & operator+= (const POINT &); // move down/right
WrapperRect & operator-= (const POINT &); // move up/left
double l, left, x; // left
double r, right; // right
double t, top, y; // top
double b, bottom; // bottom
double w, width; // width
double h, height; // height
} Rect, rect; // allow more convenient names
에 내 유일한 문제는 사용자가 있다면
Rect myRect;
myRect.right = 50;
그것이 오른쪽을 설정합니다 말을한다는 것입니다, 그러나 오른쪽 또는 너비에 대한 별칭을 변경할 수는 없습니다.
나는 회원은 개인되고 싶지 않아 중 하나를 나는 귀찮은
cout << myRect.getX();
구문보다는
cout << myRect.x;
구문을 원하기 때문에.
이를 수행 할 방법이 없나요? 아니면 get 함수를 사용해야합니까?
편집 :
필자는이 글을 쓸 때 실제로 생각하지 못했던 몇 가지 반환 값 (>.>)을 추가하고 연산자 + 등의 이중을 한 점으로 변경했습니다. 나는 하나를 받아들이 기 전에 가능성을 시험하기 시작했습니다.
정의 문에서 어리석은 오류가 나온 후 마침내 컴파일되고 작동합니다. 여기에있는 나쁜 습관에 대해 걱정하지 마십시오. 난 단지 내 작은 프로그램 개발을 위해 그것을 사용하고 있습니다. – chris
노출 된 const 참조를 const_cast로 읽기 - 쓰기 가능하게 만들 수 있습니다. – jvstech
또한 'const volatile'이어야한다는 점에 유의할 가치가 있습니다. 휘발성이 없으면 컴파일러는 역순으로 논리적으로 나타나는 다른 write-of-r보다 먼저 "x of read"를 이동할 수 있습니다. – Daniel