Class question

This is a discussion on Class question within the C++ Programming forums, part of the General Programming Boards category; This is a call I have to a function in a class. Where player[0] is an instance of that class ...

  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,046
    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 02:48 PM. Reason: nevermind

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    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, 05:08 AM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM

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