Thread: Global variable

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    133

    Global variable

    hey guys, I have been told not to use global variables in my programs but now I have come accross a problem. I have a boolean variable which is initlised to false and when a function is called it is set to true so when that function is called for a second time a certain action will occur. But i can't put the bool in that function other wise it will always reset to false.

    Is there anyother way round this problem other than having a global variable?

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Code:
    static bool var1;
    Sent from my iPadŽ

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    133
    nice one thanks.

  4. #4
    Registered User Kurisu's Avatar
    Join Date
    Feb 2006
    Posts
    62
    yep, as sly mentioned..

    A static variable is declared only once; remains active for the program life yet is only visible where it is declared.

    ex:
    Code:
    int count()
    {
      static bool status = false;  // Declared only the first time this function is called ignored each subsequent call.
    
      status = !status;
    }
    Last edited by Kurisu; 02-15-2006 at 09:05 PM.

  5. #5
    Registered User
    Join Date
    Oct 2005
    Posts
    133
    Thanks for that

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    But i can't put the bool in that function other wise it will always reset to false.
    Another way: declare your boolean variable in main(), and then send it to the function as an argument. If you make the function parameter a reference, which just means you put a '&' symbol to the right of the type, then the function will be able to change the value of the variable back in main(). Here is an example:
    Code:
    #include <iostream>
    using namespace std;
    
    void myFunc(bool& aFlag, int num)
    {
    	if(aFlag)
    	{
    		cout<<num<<endl;
    	}
    	else
    	{
    		cout<<"Flag is false."<<endl;
    		aFlag = true;
    	}
    }
    
    int main()
    {
    	bool myFlag = false;
    	myFunc(myFlag, 10);
    	myFunc(myFlag, 5);
    
    	return 0;
    }
    The parameter variable aFlag becomes a synonym, or alternate name, for myFlag back in main(), and as a result myFlag and aFlag are the same thing.
    Last edited by 7stud; 02-15-2006 at 09:38 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. global variable and prototype
    By Lincoln_Poh in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2008, 03:25 AM
  2. static class variable vs. global variable
    By nadamson6 in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2005, 03:31 PM
  3. global variable turns zero
    By Roaring_Tiger in forum C Programming
    Replies: 5
    Last Post: 04-07-2003, 12:52 PM
  4. Global variable???
    By loopy in forum C Programming
    Replies: 13
    Last Post: 06-09-2002, 03:08 PM