المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : performing operations when assigning to class member in C++



C++ Programming
09-29-2009, 02:00 AM
Hi,

I have following problem:

Suppose I have such class:

class NiceRectangle{
private:
Display dispHnd; //display handle

void DisplayRectangle(); // displays rectangle on screen

public:
unsigned int width; // dimensions
unsigned int height;
COLOR color; // interior color

unsigned int GetArea(); // return area
};


Is to it possible to perform some operations when I assign value to the width or height member.
Let`s say I write:

NiceRectangle nRect;
nRect.width = 100;

And when I assign 100 to nRect.width I want to perform instructions:
width = 100;
ShowDisplay(dispHand);
DrawFrame(width, height, Black);
...

and with
nect.height = 9;

I want to play a sound, open the fridge and finally assign 9 to height.


Another words, I want assigment operator = to act like function, but I want to be able to use it for any public member(variable).

I know it is possible to overload operator for a class, but I don`t want to overload an operator for a whole class, but only for its particular member.

I`ve heard about MFC which, I believe, performs similar tasks, but I want a more general solution, for example for linux.

Can this be done? How?

Thank You