Pointers with functions c++

This topic is 2910 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Hey I was just curious if including a pointer in a function, just to call another function is an okay idea. To understand what I mean look at the code below

//class one
//this box class has simple getter functions
class box
{
private:

//positions
int xPosition;
int yPosition;

public:

//getter functions
int getX(){ return xPosition; }

int getY(){ return yPosition; }

};

//class two
//this computer class searches for a box object, by calling its getter functions
//this class is where my question comes in
class computer
{
private:

//positions
int xPosition;
int yPosition;

//computer state
int state;

public:

//here we go, this is where I am curious

//see that I need a pointer to get the box object's x and y positions
void getBoxPos(box * BOX){ BOX->getX(); BOX->getY(); }

//in this function below, it calls 'getBoxPos(box * BOX)' depending on the integer 'state'
//so I need to include a pointer in this function, just to call 'getBoxPos(box * BOX)'
void ai(box * BOX){

switch(state){
//here
case 0: getBoxPos(BOX); break;

case 1: //do nothing
}

}

};

// p.s. I don't normally define functions in my classes, I'm just lazy today



Share on other sites

If I have guessed your true question correctly, then yes passing an argument to a function only for it to be used as a parameter to some other function is perfectly normal. In C++ it would be idiomatic to use a (const) reference for this.

If you were to give us a higher level overview of your objective, we might be able to offer more specific advice or suggest design alternatives you might not have considered.

Share on other sites
Quote:
 If I have guessed your true question correctly, then yes passing an argument to a function only for it to be used as a parameter to some other function is perfectly normal

yea, you guessed it.. thanks...

Quote:

I tried my best to make sense.