Accessing a class in a function using pointers

This is a discussion on Accessing a class in a function using pointers within the C++ Programming forums, part of the General Programming Boards category; This seems fairly easy, but I'm having a tough time accessing a class through pointers at any point in the ...

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    12

    Accessing a class in a function using pointers

    This seems fairly easy, but I'm having a tough time accessing a class through pointers at any point in the code.

    The project is an easy pet simulator, much like a tamigochi. I am trying to feed my pet through a function. I realize I can access the object from the source code and forget the function all together, but I would like to learn how this works before I go ahead.

    How I think it works is that I need to pass the address of the pet object to the function, have the function make a pointer variable to that address, and have the code alter the variables in that object through the address.

    In a case statement for user input, I have, under case 1 (Press 1 to feed pet)
    Code:
    void feedPet(beast *pet);
    And here is the function itself.
    Code:
    void feedPet(beast newPet) {
        int * pNewPet = &newPet;
        cout << "You have fed your pet 10 pieces of food ";
        *pNewPet->eat();
        return;                                
    }
    and the functions prototype:
    Code:
    void feedPet(int &pointer);

    If there is a phrase to explain what I am doing, I'd be willing to look it up. But at the moment, I have no clue what I should be looking up
    Last edited by OpiateDelusion; 06-14-2007 at 04:44 PM.

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Code:
    *pNewPet->eat();
    Lose the *.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    I would think all you'd need to do is this. Under case 1:
    Code:
        feedPet(pet);
    And the function:
    Code:
    void feedPet(beast &pet)
    {
        pet.eat();
        cout << "You have fed your pet 10 pieces of food" << endl;
    }
    Last edited by swoopy; 06-14-2007 at 04:50 PM. Reason: Deleted first example. Won't work.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    The "arrow" operator makes this
    Code:
    pNewPet->eat();
    equivalent to
    Code:
    (*pNewPet).eat();
    so there's no reason to add another *, making it
    Code:
    *(*pNewPet).eat();
    since you're not using a pointer-to-a-pointer.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    Jun 2007
    Posts
    12
    Quote Originally Posted by swoopy View Post
    I would think all you'd need to do is this. Under case 1:
    Code:
        feedPet(pet);
    And the function:
    Code:
    void feedPet(beast pet)
    {
        pet.eat();
        cout << "You have fed your pet 10 pieces of food" << endl;
    }
    You could also pass the pet by reference if it contains several data members.
    Code:
    void feedPet(beast &pet)
    {
        pet.eat();
        cout << "You have fed your pet 10 pieces of food" << endl;
    }
    It's still not looking at the address because "itsHunger" in the object is staying the same, even though it should be going down by 10 (eat() - 10 from itHunger.) It makes a new object each time it calls the function, and destroys it every time the function ends.
    Last edited by OpiateDelusion; 06-14-2007 at 04:52 PM.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Yeah sorry, it has to be a pointer or reference. Or you could make feedPet a member of the class, and do:
    Code:
    pet.feedPet();
    Last edited by swoopy; 06-14-2007 at 04:54 PM.

  7. #7
    Registered User
    Join Date
    Jun 2007
    Posts
    12
    Quote Originally Posted by swoopy View Post
    Yeah sorry, it has to be a pointer or reference. Or you could make feedPet a member of the class, and do:
    Code:
    pet.feedPet();
    Thanks for the suggestion, I plan to use it.
    I'd also like to learn how to reference an object from a function though, for later use of anything.

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >I'd also like to learn how to reference an object from a function though, for later use of anything.
    Understood. In fact I guess the same idea would apply if you needed to change a std::string in a function, or any other object from the standard library.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 04:01 PM
  3. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  4. Problem with function pointers
    By vNvNation in forum C++ Programming
    Replies: 4
    Last Post: 06-13-2004, 06:49 AM
  5. Replies: 4
    Last Post: 11-23-2003, 06:15 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21