Click here to Skip to main content
13,829,136 members
Click here to Skip to main content
Add your own
alternative version


70 bookmarked
Posted 16 Nov 1999

Saving a variable temporarily

, 16 Nov 1999
Rate this:
Please Sign up or sign in to vote.
A safe, and convenient way to store variables temporarily
<!-- STEP 2. Include download and sample image information -->
  • Download source - 1 Kb
  • <!-- STEP 3. Add the article text.-->

    On several occassions I've had to execute a piece of code which used a global variable that needed to be temporarily reassigned. The problem was that I wanted the variable to return to its original value after the code had been executed. So I would end up assigning it to some local variable, executing the code, and then setting the global variable back to its original value held by the local variable. Here's a sample of what I mean:

        // Hold value in local variable
        double* pTemp = g_pValue;
        // Reassign it to another value (temporarily)
        g_pValue = NULL;
        // Use it in some code
        FunctionWhichUsesGlobalValue( );
        // Restore it back to what it was originally
        g_pValue = pTemp;

    As may be evident, this implementation has a couple of problems. For one, if the function throws an exception the global variable never regains its original value. Another problem is that you have to remember to restore the value. For a simple case like this one it's not difficult, but if you have multiple return points, it means having to restore the value on all of them. This results in duplicate code which is more difficult to maintain.

    So how do we solve this problem? With a class, of course! If we store the variable's original value inside a class, we can then make the destructor restore the variable's value automatically. This would give us two key advantages:

    • The variable would always regain its original value.
    • We would not have to worry about explicitly restoring it.

    Now, you probably think that the way to do this is with a template class... and you're right. A template class accomplishes this task and turns the above code into something like this:

        CTemp<double*> temp = g_pValue; 
        g_pValue = NULL; 
        FunctionWhichUsesGlobalValue( );

    That's quite an improvement, isn't it?! Well, it still has one minor problem which was also prevalent in the first example: the fact that we have to know and specify the variable's type. And while that is not too big of a deal, wouldn't it be better to not have to do it? In other words, wouldn't it be nice to just say: CTemp temp = g_pValue and the object would then somehow know that g_pValue is a pointer to a double? Well, with template member functions it can be done!

    Template member functions work just like regular template functions. The compiler generates them based on the type of the arguments on which they operate. So what is basically needed is a class with a template constructor and a way for the constructor to store the value in a generic member variable which the destructor can then properly restore. It's a bit tough to explain the exact mechanism here but I think you'll understand it once you see its implementation. The class is called CTemp and applying it to the code above would make it look like this:

        CTemp temp = g_pValue; 
        g_pValue = NULL; 
        FunctionWhichUsesGlobalValue( );

    Much nicer, don't you think?! Not only does this class do what the template version did but it does it without you having to determine and then specify the variable's type in the code. That's what I call putting the compiler to work for the programmer!

    To use the class simply download the temp.h file into your C++ project, include it where you need it, and enjoy!


    This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

    A list of licenses authors might use can be found here


    About the Author

    Alvaro Mendez
    Web Developer
    United States United States
    I've done extensive work with C++, MFC, COM, and ATL on the Windows side. On the Web side, I've worked with VB, ASP, JavaScript, and COM+. I've also been involved with server-side Java, which includes JSP, Servlets, and EJB, and more recently with ASP.NET/C#.

    You may also be interested in...

    Comments and Discussions

    GeneralMy vote of 4 Pin
    BillW3323-Sep-10 4:22
    professionalBillW3323-Sep-10 4:22 
    GeneralNice Pin
    Stephen Hewitt26-Jan-06 16:20
    memberStephen Hewitt26-Jan-06 16:20 
    Generalcode in saving vb Pin
    princess marla sarmiento3-Feb-04 17:02
    sussprincess marla sarmiento3-Feb-04 17:02 
    Questionwhy? Pin
    Jeff Reed4-Feb-00 13:14
    memberJeff Reed4-Feb-00 13:14 
    AnswerRe: why? Pin
    Alvaro Mendez8-Feb-00 10:31
    memberAlvaro Mendez8-Feb-00 10:31 
    GeneralRe: why? Pin
    Mike Junkin3-May-00 11:50
    memberMike Junkin3-May-00 11:50 
    GeneralRe: why? Pin
    Alvaro Mendez3-May-00 12:46
    memberAlvaro Mendez3-May-00 12:46 
    GeneralRe: why? Pin
    Soeren18-May-00 7:11
    memberSoeren18-May-00 7:11 
    GeneralRe: why? Pin
    5-Feb-01 22:55
    suss5-Feb-01 22:55 
    AnswerRe: why? Pin
    Stephen Hewitt26-Jan-06 16:22
    memberStephen Hewitt26-Jan-06 16:22 

    General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

    Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

    Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
    Web05 | 2.8.190114.1 | Last Updated 17 Nov 1999
    Article Copyright 1999 by Alvaro Mendez
    Everything else Copyright © CodeProject, 1999-2019
    Layout: fixed | fluid