Sign in to follow this  
nuclearfission

Pointers with functions c++

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 this post


Link to post
Share on other sites
Your example is somewhat confusing.

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 this post


Link to post
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:

Your example is somewhat confusing.


I tried my best to make sense.

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