I made a class, and I know what the problem is, but I don't know how to fix the problem the way I want to. I want to compare a pointer to an int, but without creating a new variable. For my case, doing this is very trivial, as I do not need to worry about operation time on such a small game as mine, but I just want to get in the habit of creating efficient code, whether I need it or not.
Now nudging on, I have made a class, and I have found the problem from the compiler error, I am not allowed to compare a pointer to a int, but is there some way to get around this without making a new variable, as the whole point is of me using a pointer is to conserve memory.
Here's my class.
Code:class pong_movement { public: void set_cords(int pong_x, int pong_y); // points pointers void speed_x(); //chages outside pong positions void speed_y(); private: int *ptr_x; // to acces pong pos out of class int *ptr_y; bool x_dir; // tells which way pong is travling bool y_dir; }; void pong_movement::set_cords(int pong_x, int pong_y) { ptr_x = &pong_x; ptr_y = &pong_y; y_dir = false; // y starts at top and moves down, thus starts false x_dir = true; } void pong_movement::speed_x() { if (ptr_x <= 0 | x_dir) // { ptr_x++; x_dir = true; } else if(ptr_x >= 455 | !x_dir) { ptr_x--; x_dir = false; } } void pong_movement::speed_y() { if (ptr_y <= 0 | !y_dir) // { ptr_y++; y_dir = false; } else if(ptr_y >= 615 | !y_dir) { ptr_y--; y_dir = true; } }
Will the pointers be assigned the address of the outside vars, or does the class create a new variable when a the function is called? I will initiate the class at the beginning of the program with pong::set_cords then I will use speed_x/y in a while loop of the game. Will this work behind the whole theory of my class?
Thanks,
Joe