Sign in to follow this  
daniel_i_l

passing a pointer to a function

Recommended Posts

daniel_i_l    295
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.

Share this post


Link to post
Share on other sites
Kambiz    758
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 : 1
Sensor Type : 0


As you can see everything works like expected so the bug must be somewhere else.

Share this post


Link to post
Share on other sites
jyk    2094
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this