GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 09-Jan-2005, 17:01
small_ticket small_ticket is offline
Junior Member
 
Join Date: May 2004
Posts: 45
small_ticket is on a distinguished road

pointer to a member function


Hi!
i have a pointer
CPP / C++ / C Code:
ListNode *temp;

and i send this pointer to a member function commandGoto in this part of code
CPP / C++ / C Code:
	ListNode *temp;
	temp=listObject.getFirstPtr();
	while(temp->getNextPtr() != 0) 
	{
		int control=0;
		char * command;
		char * tempString=temp->getData();
		command=strtok( tempString ," ");
		
		//cout<< command<< "\n";
		
				
		switch (*command)
		{
		case 'R':
			
			break;
		
		case 'I':
			
			if (*(command+1)=='f'){
				
				commandIf( &tempString[3], temp );
				control=1;
			}
			
			else if (*(command+1) == 'n'){

				commandInput( tempString[6] );
				control=1;
						
			} else{
				cout<<"Invalid command";
				exit(1);
			}

			break;
		
		case 'L':
			
			commandLet ( &tempString[4] );
			control=1;
			break;
		
		case 'P':
			
			commandPrint( tempString[6] );
			control=1;
			break;
		
		case 'G':
			
			commandGoto ( &tempString[5], temp  ); 
			break;
		
		case 'E':
			
			cout<<"End of the program, terminate!!";
			break;
		
		default:
			cout<<"Wrong command!!";
			break;
		}

		if (control)
			temp=temp->getNextPtr(); 
	}	//end of while
}
and the commandGoto member function is like this
CPP / C++ / C Code:
void Interpreter :: commandGoto (char * lineToGo, ListNode * temp  )
{
		int destinationLine = atoi (lineToGo);

		temp=listObject.getFirstPtr();
	
		while ( temp->getLineNo() != destinationLine )
		temp=temp->getNextPtr();
	
		
}
this member function takes the argument LineToGo and goes to the node which has the destination line number. i want this function to change ListNode temp; in the place that i call the commandGoto function.

i think it is something related to call by reference but i could not realize how i shall do this.

thanks...
  #2  
Old 09-Jan-2005, 18:04
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,693
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by small_ticket
Hi!
i have a pointer
CPP / C++ / C Code:
ListNode *temp;

and i send this pointer to a member function commandGoto in this part of code

...

this member function takes the argument LineToGo and goes to the node which has the destination line number. i want this function to change ListNode temp; in the place that i call the commandGoto function.

i think it is something related to call by reference but i could not realize how i shall do this.

thanks...


Three ways come to mind:

1. Have the function commandGoto() return a pointer to ListNode, then
CPP / C++ / C Code:
 case 'G':
      
      temp = commandGoto ( &tempString[5], temp  ); 
      break;

CPP / C++ / C Code:
ListNode * Interpreter :: commandGoto (char * lineToGo, ListNode * temp  )
{

..
..
..
  return temp;
}

2. (The old-fashioned C way) Make the second argument of commandGoto() a pointer to a pointer to ListNode and invoke it with &temp (where temp is a pointer to ListNode)

3. (The new-fangled C++ way) Make the second argument of commandGoto() a reference variable. The effect will be the same as number 2.,above, but the notation will be slightly less confusing (according to some people).

In cases like this, where the function only has to return one value, I personally (usually) prefer number 1. That's what functions are really good at: returning a value. (But that's just me --- Your Mileage May Vary).

Regards,

Dave
  #3  
Old 09-Jan-2005, 19:09
small_ticket small_ticket is offline
Junior Member
 
Join Date: May 2004
Posts: 45
small_ticket is on a distinguished road
before i had posted my question i have tried all of the things that you have said (davekw7x) but in the first way that you told i had forgotten to make
CPP / C++ / C Code:
 void Interpreter :: commandGoto (char * lineToGo, ListNode * a )
as
CPP / C++ / C Code:
 ListNode * Interpreter :: commandGoto (char * lineToGo, ListNode * a  )

Now it works fine thanks!!! But i have a quesitonmark in my mind?? i did try the other two several times and also after you said (davekw7x) but they do not work!! is there any reason for that come to your mind??*

Again thanks a lot for your assist...
  #4  
Old 10-Jan-2005, 07:23
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,693
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by small_ticket
But i have a quesitonmark in my mind?? i did try the other two several times and also after you said (davekw7x) but they do not work!! is there any reason for that come to your mind??*

Again thanks a lot for your assist...

Let's see, now --- I'll make a list of all of the ways that something won't work. On second thought, forget it: life is just too short.

But seriously, folks, they can work; making them work is left as an exercise for the student. Using pointers to pointers is easy to mess up. Using reference variables is easy to mess up. Don't ask me to list all of the ways that I have ever messed up. Either method can be used when you want a function to return more than one thing.

Regards,

Dave
 
 

Recent GIDBlogWelcome to Baghdad by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
[Include] Doubly-linked List dsmith C Programming Language 6 14-Apr-2006 13:12
[Tutorial] GUI programming with FLTK dsmith FLTK Forum 10 03-Oct-2005 15:41
Why static member function cannot be virtual in C++ Poolan C++ Forum 1 20-Nov-2004 09:51
Revising Script style ?????? pepee MySQL / PHP Forum 4 14-Apr-2004 04:59

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 20:01.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.