Incrementing a number each time a function is called.

This is a discussion on Incrementing a number each time a function is called. within the C++ Programming forums, part of the General Programming Boards category; Wow, I dunno what I was thinking recommending using a static variable. If I recall correctly it was the OP's ...

  1. #16
    Registered User Scribbler's Avatar
    Join Date
    Sep 2004
    Location
    Aurora CO
    Posts
    266
    Wow, I dunno what I was thinking recommending using a static variable. If I recall correctly it was the OP's statement that he couldn't use an algorithm (which I took to mean he couldn't use an expression like num = intUp( num ); ) which lead me in that direction. Ideally I'd simulate passing by reference with a pointer.

    However with the attempt to use static above, here's a hint... If you declare and initialize a static variable inside a function, it's only initialized once (first use), so subsequent calls to that function will not re-initialize the variable. Additionally it does not get cleared from memory when the function block is exited, and still retains it's scope (meaning once you leave that block, you can't access it until you re-enter that block).

  2. #17
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why not use a static variable?
    Code:
    void countme( void )
    {
        static unsigned int var;
        var++;
    }
    Now, if you do want to have the ability to reset it, do something like this:
    Code:
    void countme( bool reset = 0 )
    {
        static unsigned int var;
        if( reset )
            var = 0;
        var++;
    }
    The static variable works just fine for keeping track of how many times a function is called. You just have to use it correctly. (If the default assignment is wrong to 'reset', it's because it's extremely rare that I ever use C++.)

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #18
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>If the default assignment is wrong to 'reset', it's because it's extremely rare that I ever use C++.
    Well, technically it should be = false (I think there was an argument about assigning integer values to bools some time ago), but practically speaking you'll never have a problem.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 12:49 PM
  2. Sending an email in C program
    By Moony in forum C Programming
    Replies: 28
    Last Post: 10-19-2006, 10:42 AM
  3. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. help with a source code..
    By venom424 in forum C++ Programming
    Replies: 8
    Last Post: 05-21-2004, 12:42 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21