Skip to main content
gamedev.net
🔒 Locked

passing a pointer to a function

Started by daniel_i_l 2 replies 450+ views
Original Post
daniel_i_l
daniel_i_l
I have a program like this:

void TryTriggerSystem(void)
	{
	Trigger *MyTrig;
	MyTrig=new Trigger;
	Operator *MyAnd=CreateOperator(D_OP_AND);
	Operator *MyOr=CreateOperator(D_OP_OR);
	Operator *MyOr2=CreateOperator(D_OP_OR);
	Sensor *MyOne=CreateSensor(D_SNS_ONE);
	Sensor *MyZero=CreateSensor(D_SNS_ZERO);
	Action *MyState=CreateAction(D_PRINTSTATE);

	MakeLink(MyTrig,MyState);
	MakeLink(MyTrig,MyAnd);
		MakeLink(MyAnd,MyOr);
			MakeLink(MyOr,MyOne);
			MakeLink(MyOr,MyZero);
		MakeLink(MyAnd,MyOr2);
			MakeLink(MyOr2,MyOne);
			MakeLink(MyOr2,MyOne);
		MakeLink(MyAnd,MyOne);

	ChangeSensorType(MyOne,D_SNS_ZERO); 
    // This line has a BUG! : MyOne dosn't change at all !!!!

	MyTrig->Go();
	}

Sensor* CreateSensor(int TYPE)
	{
	Sensor *nSns;
	switch(TYPE)
	 	{
		case D_DEFAULT:
		case D_SNS_ONE:
			nSns=new SenseONE();
			break;
		case D_SNS_ZERO:
			nSns=new SenseZERO();
			break;
		default:
			nSns=NULL;
			break;
		}
	return(nSns);
	}
void ChangeSensorType(Sensor *Sns,int TYPE)
	{
	//delete Sns;	
	Sns=CreateSensor(TYPE);
	}

There're some stuff there that aren't clear without the rest of the program but the problem is in the last line of the first function. When I pass MyOne to the ChangeSensorType it's type doesn't change. Why not? Thanks.
"We've all heard that a million monkeys banging on a million typewriters will eventually reproduce the entire works of Shakespeare. Now, thanks to the internet, we know this is not true." -- Professor Robert Silensky
Kambiz
Kambiz
I have write a simple program to test your functions:
#include <iostream>class Sensor {public:	virtual int Type()=0;};enum{D_DEFAULT,D_SNS_ONE,D_SNS_ZERO};class SenseONE:public Sensor{	int Type()	{		return 1;	}};class SenseZERO:public Sensor{	int Type()	{		return 0;	}};Sensor* CreateSensor(int TYPE);void ChangeSensorType(Sensor *Sns,int TYPE);void TryTriggerSystem(void){	Sensor *MyOne=CreateSensor(D_SNS_ONE);	std::cout<<"Sensor Type : "<<MyOne->Type()<<std::endl;	ChangeSensorType(MyOne,D_SNS_ZERO); 	std::cout<<"Sensor Type : "<<MyOne->Type()<<std::endl;}Sensor* CreateSensor(int TYPE)	{	Sensor *nSns;	switch(TYPE)	 	{		case D_DEFAULT:		case D_SNS_ONE:			nSns=new SenseONE();			break;		case D_SNS_ZERO:			nSns=new SenseZERO();			break;		default:			nSns=NULL;			break;		}	return(nSns);	}void ChangeSensorType(Sensor *Sns,int TYPE)	{		delete Sns;	Sns=CreateSensor(TYPE);	}void main(){	TryTriggerSystem();}


Output:
Sensor Type : 1Sensor Type : 0


As you can see everything works like expected so the bug must be somewhere else.
Zakwayda
Zakwayda
Based on what you posted there, I'm guessing you actually want:
void ChangeSensorType(Sensor *&Sns,int TYPE)
Keep in mind though that all that pointer manipulation can get pretty dicey, and isn't really 'best practice' as far as modern C++ goes.
daniel_i_l
daniel_i_l
Thanks
"We've all heard that a million monkeys banging on a million typewriters will eventually reproduce the entire works of Shakespeare. Now, thanks to the internet, we know this is not true." -- Professor Robert Silensky

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.