static

This is a discussion on static within the C++ Programming forums, part of the General Programming Boards category; What this keyword really does? I read somewhere it makes vars file-scope. And somewhere else I heard it makes vars ...

  1. #1
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231

    static

    What this keyword really does?
    I read somewhere it makes vars file-scope. And somewhere else I heard it makes vars to live even after the end of the block.
    Now, what is the right explanation?
    Last edited by siavoshkc; 02-01-2006 at 05:40 AM.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  2. #2
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Both

  3. #3
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231
    It makes vars filescope even if it is used in a block?
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,799
    Quote Originally Posted by siavoshkc
    It makes vars filescope even if it is used in a block?
    Depends on if you declare it within that block of code, or outside of any function block (similar to how a basic global is declared). Declared within a function for example the value of the static variable will persist between function calls and be visible (in scope) within that function. Declared outside of any function (as globals are) it makes the variable have file scope (like a global but only to that source file/translation unit and not visible to other files via the use of the extern keyword).
    Last edited by hk_mp5kpdw; 02-01-2006 at 05:46 AM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231
    So it has really two meanings.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    And somewhere else I heard it makes vars to live even after the end of the block.
    That's true, but the static var is not accessible outside the block. So, this doesn't work:
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	
    	{
    		static int num = 10;
    	}
    
    	cout<<num<<endl;
    
    	return 0;
    }
    Hmmm...so what good is a static variable? How do you get back inside a block to access the static var after you have exited the block? Ahah! A function can enter and exit a block more than once:
    Code:
    #include <iostream>
    using namespace std;
    
    
    void myFunc()
    {
    	static int num = 0; //only executed the first time through the function
    
    	++num;
    
    	cout<<num<<endl;
    }
    
    int main()
    {
    	
    	myFunc();  //1
    	myFunc();  //2
    
    	return 0;
    }
    Last edited by 7stud; 02-01-2006 at 09:59 AM.

  7. #7
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231
    Understood. If we use static in functions (any block) the value of var wont be changed between the calls.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  8. #8
    Registered User
    Join Date
    Oct 2005
    Posts
    88
    That's right. It means static vars are good for saving information between class instances too.

  9. #9
    Registered User
    Join Date
    Mar 2002
    Posts
    203

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Note that the second meaning, (file scope) is deprecated in C++ in favor of using an unnamed namespace.

    As noted in the link above, the static keyword is also used with classes/structs to indicate that a member variable will have only one instance shared across all instantiations of the class, and similarly a member function does not have a this pointer and cannot access the data of any particular instantiation.

  11. #11
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231
    Let me read the link, then I'll come back.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  12. #12
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231
    OK, tnx.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  3. LNK2001 ERROR!!! need help
    By lifeafterdeath in forum C++ Programming
    Replies: 7
    Last Post: 05-27-2008, 05:05 PM
  4. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 04:42 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM

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