Wednesday, January 20, 2010

The easy way to call pre and post function execution code pieces

Well at times you want to execute some while you enter some function, something like enable or disable certain objects or set some flags before execution and reset them after you have finished this function.

The most straightforward way of doing it is putting code for it in the beginning and end of the function. But if this code of your grows in size and there are several return statements added to it, then you maintaining this code will become difficult.

So the easy way out ? Well its simple. Create a new class with just constructor and destructor. In the constructor call the code which you want to execute initially, and the destructor to have the post processing code. Make sure that copy constructors is made private just to avoid complications.


class magicalObject
            // do pre processing job here
            // do post processing job here
        magicalObjet(magicalObject & obj);      

void somefunction()
    magicalObject myMagicalObject;     // pre processing job done now

    // do your job

    // no worries the post processing job will be done my
    // magical object


aftaab4u said...

hi friend i have problrm was wondering if u can help...
i m trying to learn C
i cant find a "compatible complier " for my vista home basic 64bit and also there is no way i can format to xp as i use a cq50-106au only vista compatible..
tried installing Turbo C, GCC, mini GW and lccwin64 but still same error " application fail "The version of this file is not compatible with the version of windows you're running. Check your computer’s system information to see whether you need an x86 (32-bit) or x64 (64-bit) version of the program, and then contact the software publisher."

pease help

thanks and regards,

100rabh™ said...

try using Visual Studio express edition. You can get it at

Anonymous said...

Hello 100Rab
i like your blog for 2 reasons.
one is its black back ground which saves a hell lot of power.
and other is the C++ snippets which are very much informative .

i appreciate your effort and motivation behind it !!

Kudos to you my friend.