Monday, 2 March 2009

Hacking the Heart

"If open bracket love equal equals true, my heart's pointer is forever assigned to you".

The only way to be forever assigned to someone, is by using a const pointer.
So say we had a heart inside of us..

 class Heart  
 {  
 }; 
 class Us  
 {  
 public:  
      Us()  
      {  
           myHeart = new Heart();  
      }  
      ~Us()  
      {  
           delete myHeart;  
      }  
      const Heart* GetConstHeartPtr()  
      {  
           return myHeart;  
      }  
 private:  
      Heart* myHeart;  
 };  


In our main function, since our hearts are constant, there would be no legal way to fill them. With the only solution being to hack our hearts with memcpy, and write over our heart with their heart.

 int _tmain(int argc, _TCHAR* argv[])  
 {  
      Us *me = new Us();  
      Us *you = new Us();  
      if( me != NULL && you != NULL )  
      {  
           memcpy( me, you, sizeof( Heart ) );  
           if( me->GetConstHeartPtr() == you->GetConstHeartPtr() )  
           {  
                printf( "Love" );  
           }  
      }  
 }  


Hacky but, it still doesn't work. Why? Because our hearts are empty to start with.
Now, if we go back to the start and fill our hearts with something.

 class Heart  
 {  
 public:  
      Heart(const char *name) : name( name )  
      {}  
 private:  
      const char *name;  
 }; 
 class Us  
 {  
 public:  
      Us(const char *name)  
      {  
           myHeart = new Heart( name );  
      }  
      ~Us()  
      {  
           delete myHeart;  
      }  
      const Heart* GetConstHeartPtr()  
      {  
      return myHeart;  
      }  
 private:  
      Heart* myHeart;  
 };  


We'd have something to allocate, because our heart would point to a memory location which contains a name, which can be created with malloc and filled.

 int _tmain(int argc, _TCHAR* argv[])  
 {  
      Us *me = new Us( "me" );  
      Us *you = new Us( "you" );  
      if( me != NULL && you != NULL )  
      {  
           memcpy( me, you, sizeof( Heart ) );  
           if( me->GetConstHeartPtr() == you->GetConstHeartPtr() )  
           {  
                printf( "Love" );  
           }  
      }  
 }  


So what's the lesson to be learned?
Don't try to love a girl that's heartless.