Thread: Question about Static variables and functions.

  1. #1
    1479
    Join Date
    Aug 2003
    Posts
    253

    Question about Static variables and functions.

    What is the different between a function that uses static variables and a static function? I know that a function that has static variables more or less keeps a 'running' total of the variable(s) inside it. What I am confused on is what a static function is meant to do. Is this a static function prototype?
    Code:
    static int add(int, int);
    Or is a function just called static becase it uses static variables? If they are two different things could some one point me in the direction of where I could see some examples? Also, is there a purpose to use a static variable if main() is the only function in the program?
    Knowledge is power and I want it all

    -0RealityFusion0-

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >What is the different between a function that uses static variables and a static function?
    The difference is that static local variables are not deprecated and static functions are. So you shouldn't be using static functions in C++ anyway. But if you must know, a static function has internal linkage instead of the default external linkage. That means that only code from the same translation unit (aka. file after preprocessing) can see and use that function when it's declared as static. The C++ equivalent is an unnamed namespace:
    Code:
    namespace {
      int add(int, int);
    }
    My best code is written with the delete key.

  3. #3

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. question about static members and functions
    By e66n06 in forum C++ Programming
    Replies: 5
    Last Post: 01-07-2008, 02:41 PM
  2. Static variables in functions
    By tretton in forum C Programming
    Replies: 1
    Last Post: 04-08-2006, 06:49 AM
  3. Using private class members in static functions
    By sethjackson in forum C++ Programming
    Replies: 2
    Last Post: 09-23-2005, 09:54 AM
  4. Static variables and functions problem in a class
    By earth_angel in forum C++ Programming
    Replies: 16
    Last Post: 09-15-2005, 12:08 PM
  5. Static variables in functions
    By PJYelton in forum C++ Programming
    Replies: 2
    Last Post: 10-04-2002, 01:41 PM