An Invariant of an object is something that is true before you manipulate the object and after you manipulate the object.
Example:
You have a class car like this:
1 2 3 4 5 6 7 8 9 10
class Car
{
public:
Car(Engine* e) : _e(e) {assert(e != 0);};
void startEngine() {_e->start();};
void removeEngine() {_e = 0;};
private:
Car(); // prevent creation of Cars without engine
Engine* _e;
};
The constructor establishes the invariant "a Car has an engine".
The method "startEngine" relies on this invariant.
The method "removeEngine" violates the invariant. That's why a subsequent call to "startEngine" will fail!
but am unable to understand the code of invariant.
i understood what an invariant is.
Invariant is a specific rule that an object has to satisfy in order to be created or manipulated.
thank you ceruleus for the google link.
fool, did u think that i didn't google stuff before asking here?
do u think that i don't have any other work or i feel gud to waste time asking stupid questions?