Thread: Class question

  1. #1
    Registered User
    Join Date
    Jan 2006
    Location
    Boston, Massachusetts
    Posts
    23

    Class question

    This is a call I have to a function in a class. Where player[0] is an instance of that class and bet_or_pass() is a public function in that class.
    Code:
    players[0].bet_or_pass();
    So how would I set a variable in that function to the instance that called the function. For example, I want to set a global variable to be the instance of the class(player[0]) that called the function. So, I guess im asking how do you make a self reference in a class function when you call the function like I have shown.

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    The this 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.

  3. #3
    Registered User
    Join Date
    Jan 2006
    Location
    Boston, Massachusetts
    Posts
    23
    Quote Originally Posted by dwks
    The this pointer.
    see thats what I thought. But then I did this in the public function bet_or_pass() in the class Player before I posted:
    Code:
    high_bidder = this;
    and got this compile error:
    Code:
    game.cpp: In member function `void Player::bet_or_pass()':
    game.cpp:266: no match for `Player& = Player* const' operator
    game.cpp:42: candidates are: Player& Player::operator=(const Player&)
    I initialized the global variable high_bidder like this:
    Code:
    Player high_bidder;
    [edit] I re-read what you put and you said pointer. So, I just did this:
    Code:
    high_bidder = *this;
    and it worked. Thanks for the help. The keyword was pointer. haha.
    [/edit]
    Last edited by pjharris; 01-07-2006 at 03:48 PM. Reason: nevermind

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Be careful. You are making a copy of the player and storing it in high_bidder. That means that if the player's internal data changes later, it will not be reflected in the high_bidder variable. It also means that if you change the data in the high_bidder variable, it will not modify the player that you assigned to it.

    Usually you want to store some sort of reference to the player that is the high_bidder, instead of copying the data. To do this, you could make high_bidder a pointer, since it will point to the same instance as the player you assign to it. You could also make high_bidder store the player id or the player name (if the names are unique) so that you can look up the appropriate player later to act on.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM
  2. Replies: 8
    Last Post: 10-02-2005, 12:27 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. question about DLL's and class functions
    By btq in forum Windows Programming
    Replies: 2
    Last Post: 02-25-2003, 06:08 AM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM