Thread: compare pointer to int without new var

  1. #1
    NotSoAvgProgrammer
    Join Date
    Jul 2007
    Location
    Virginia, U.S.
    Posts
    57

    compare pointer to int without new var

    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

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    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;
    }
    In this code, pong_x and pong_y are copies of the passed in arguments and are basically temporaries that are pushed and popped onto and off of the stack like most function variables. Setting your ptr_x and ptr_y data members to the address of these temporaries means once the function exits, they will be pointing to invalid addresses.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    NotSoAvgProgrammer
    Join Date
    Jul 2007
    Location
    Virginia, U.S.
    Posts
    57
    Well is there a solution? Can I somehow pass the address rather than the value?
    If not, is there a solution to my main question?

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    If you need to, you could have the arguments be pointers instead of straight copies. When calling the function you would pass the argument's address using the address-of operator (&).
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by avgprogamerjoe View Post
    I want to compare a pointer to an int
    Unless you can provide an EXTREMELY good explanation of why you want to do that, I'd say your design is just broken, period. What makes you think you need to compare a pointer to an integer?

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Yes it is broken, and this is where it is broken
    > int *ptr_x; // to acces pong pos out of class
    The whole point of a class is that the data is hidden. It does not depend on the outside world, and nor can the outside world change it.
    The entire interface to access and modify the data is via the member functions.

    > if (ptr_x <= 0 | x_dir)
    1. if ( *ptr_x <= 0 | x_dir) would make it at least syntactically correct.
    2. Read up on the difference between | and || operators.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory leak
    By aruna1 in forum C++ Programming
    Replies: 3
    Last Post: 08-17-2008, 10:28 PM
  2. Game Won't Compile
    By jothesmo in forum C++ Programming
    Replies: 2
    Last Post: 04-01-2006, 04:24 PM
  3. Converted from Dev-C++ 4 to Dev-C++ 5
    By Wraithan in forum C++ Programming
    Replies: 8
    Last Post: 12-03-2005, 07:45 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM