Importance of TypeSafety -An example

class Bank
{

private:
bool bFirstLock;
bool bSecondLock;
BankVault* pBankVault;

public:

Bank()
{
bFirstLock=0;
bSecondLock=0;

pBankVault= new BankVault ;
}

bool IsLocked()
{
if(( true == bFirstLock) && (true== bSecondLock))
{
printf("Both Locks unlocked- Bank is UNLOCKED\n");
return false;
}
else
{
printf("Bank is LOCKED\n");
return true;
}
};


};

//-----------------------------------------------------
class Robber
{
short ForgedKey;
void* pAceesor;

public:

Robber()
{
ForgedKey=0;
};

RemoveLocks()
{
//This method has the instruction to set the first
// 2 bytes (short) of the start of the class to value
//257; This in binary is 00000001 00000001;ie the first 2 bytes to 1 ;
//But since we are using the pointer of the Bank object to call
//this method, it sets the first 2 bytes of the object of
//bank to 00000001 00000001; But in the Bank object the first 2
//bytes are occupied by two boolean variables; Hence this method
//in effect sets the booleans to true

ForgedKey=257;

}
GetCahFromVault()
{
// pAceesor=
}

};
//------------------------------------------------------------

void Run()
{


Bank* pBank = new Bank ;
Robber* pRobber =0;//


pBank->IsLocked(); //?? Output is bank is locked

pRobber= reinterpret_cast (pBank);

pRobber->RemoveLocks();

printf("After wrong type casting\n");

pBank->IsLocked(); //?? Output is bank is UN LOCKED

return;

}

Comments

Popular posts from this blog

Long running Java process resource consumption monitoring , leak detection and GC tuning

Best practises - Selenium WebDriver/ Java

CORBA - C++ Client (ACE TAO) Java Server (JacORB) A simple example