私は自分自身のWindows APIラッパーを開始していますが、C++機能を組み込むように構造を書き直しながら、慣れ親しんだトピックを実行しました。データメンバーをどこからでもアクセスできるようにしますが、読み取り専用
私はこの回している:私の唯一の問題は、ユーザが
Rect myRect;
myRect.right = 50;
を言っていたならば、それは右サイドを設定することで、
#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
に
typedef struct _RECT {
LONG left;
LONG top;
LONG right;
LONG bottom;
} RECT, *PRECT;
を右側またはエイリアスのエイリアスを変更することはできません。
私は
cout << myRect.x;
構文ではなく、迷惑
cout << myRect.getX();
構文をしたいので、メンバーはいずれかのプライベートになりたくありません。
これを達成する方法はありますか、またはget関数を使用する必要がありますか?
編集:
私はこれを書いたとき、私はいくつかの戻り値(>。>)を追加し、演算子+などの倍精度をポイントに変更しました。私はそれを受け入れる前に可能性を試しています。
定義文から愚かなエラーが出た後、私はついにそれをコンパイルしてくれました。ここの悪い習慣を心配しないでください。私は自分の小さなプログラム開発のためだけに使っています。 – chris
公開されているconst参照をconst_castで読み書き可能にすることができます。 – jvstech
また、 'const volatile'でなければならないことに注目する価値があります。揮発性がなければ、あなたのコンパイラは逆の順序で論理的に現れる他の書込みの前に "read of x"を動かすかもしれません。 – Daniel