Thread: Is point to this variable?

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    19

    Is point to this variable?

    Edit: Title supposed to be "Is there a point", sorry.

    So my book has this example program meant to show how to use stacks and queues. It uses a stack to determine if a group of characters in a queue is parenthetically correct(every "(" has a matching ")" in the right place, etc). Here's the function:

    Code:
    template <class T>
    bool testQueue( Queue<T> &q )
    {
    	const string balTokenTableOpen ( "{[(" );
    	const string balTokenTableClose( "}])" );
    	const string quoteTokens( "'\"" );
    	Stack<T> s;
    	char c, cl;
    	string test; //What's the point of this variable?
    	int pos;
    	bool esc = false;
    	bool OK = true;
    
    	while ( !( q.isEmpty() ) && OK )
    	{
    		q.dequeue( c );
    		test = c;  // assigned to c here
    
    		if ( esc )  // ignore the character after the escape character
    		{
    			esc = false;
    			continue;
    		}
    
    		if ( c == '\\' )
    		{
    			esc = true;
    		}
    		else if ( balTokenTableOpen.find( test ) != string::npos ) //Why can't you just use c instead of test?
    		{
    			s.push( c );
    		}
    		else if ( ( pos = balTokenTableClose.find( test ) ) != string::npos  ) //Used again here.
    		{
    			if ( s.isEmpty() )
    				OK = false;
    			else
    			{
    				s.pop( cl );
    				if (  balTokenTableOpen.at( pos ) != cl )
    					OK = false;
    			}
    		}
    		else if ( quoteTokens.find( test ) != string::npos ) // And again.
    		{
    			char temp;			
    			s.getTop( temp );  // No need to check catch an error here because
    			
    			if ( s.isEmpty() || temp != c ) // we will catch empty stack here.  Notice how short-circuit evaluation is used.
    				s.push( c );
    			else
    			{
    				s.pop();
    			}
    		}
    	}
    
    	if ( esc || !( s.isEmpty() ) )
    		OK = false;
    
    	return OK;
    }
    I'm wondering why the program declares the string "test" and uses it when it seems like it could just use "c" instead. The book claims that there is a reason, but doesn't specify it. I tried to figure it out, and I even replaced all uses of "test" with "c", ran the program, and it worked exactly the same with no errors. I see absolutely no point in using test. Is there something I'm missing? The book is big on putting things into code because it's a good "design choice" but I can't see why declaring a seemingly useless variable could be a good idea.
    Last edited by Freddy92; 10-12-2011 at 04:42 PM.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    You cannot do
    test = c;
    if c is a char and test is a string. You should get an error.

    Similarly you cannot compare a string type to a single char.

    I can only say test is useless because it is being used in a broken fashion, anyway.

  3. #3
    Just a pushpin. bernt's Avatar
    Join Date
    May 2009
    Posts
    426
    You cannot do
    test = c;
    if c is a char and test is a string. You should get an error.
    Sure you can.

    ---
    WRT the code, I don't know why he used test. Maybe he just didn't know about string::find(char). Depending on the age of the book, it would seem to me he didn't know about STL's queue and stack either.

    (On a similar note, if the book is old, perhaps the standard has changed since it was published? Maybe string::find(char) didn't exist then? I can't find any 'C++ changelog' to refer to for this but it's certainly possible.)

    EDIT: Like whiteflags said, though, this code is broken. It accepts a templated Queue<T> as a parameter and then incorrectly assumes T is type char when getting elements from the queue.
    Last edited by bernt; 10-12-2011 at 05:37 PM.
    Consider this post signed

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 06-30-2011, 03:24 PM
  2. Replies: 2
    Last Post: 04-27-2011, 04:34 AM
  3. Point to Point Protocol and Bluetooth DUN
    By PSLoh in forum Networking/Device Communication
    Replies: 2
    Last Post: 03-03-2008, 09:44 AM
  4. Replies: 15
    Last Post: 09-30-2004, 10:48 AM
  5. Floating point faster than fixed-point
    By VirtualAce in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 11-08-2001, 11:34 PM