Should this function be a member function or friend?

So I have a function that looks like this at the moment:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void Player::Betting () {
  do {
    std::cout << "\n Place your bets: ";
    std::cin >> myBet;
    
    if (myBet <= myMoney) {
      std::cout << "\n Thank you, sir.\n";
    }
    else
      std::cout << "\n I'm sorry sir, but you do not have that much money.\n";
    
  } while (myBet > myMoney);
  myMoney -= myBet;
}


I have another class, Table, which handles most things related to the actual gaming processes (the Player class simply handles his own money). The Player class only has two data members: myBet and myMoney, so it would make sense to have this function in that class, but its content suggests it might as well be in the Table class.

If I put it in the Table class, I would have to give the function the argument (Player& aPlayer) and then use functions like GetBet() and GetMoney() to access the private data members of Player. Alternatively, I could simply make Betting a function of its own and declare it in the Player class as

void friend Betting(Player&);

Which would be preferable, in terms of "good" (object-oriented) programming? Member function in Player class, member function in Table class, or a friend function for Player class?
Last edited on
the necessity for friends is surprisingly rare. If you find yourself having to ask whether or not a function/class should be a friend, it probably shouldn't be. My first instinct would be to make it a Player member.

You do make a really good point about the content suggesting it belong to Table. However since it doesn't actually use anything from Table it doesn't make a lot of sense to put it there, IMO. The only thing I can think of that would come from Table would be the output strings (if you wanted to make those variable -- like if dealers at different tables said different things in response to your bet).

Disch: My thoughts exactly, and your response provides with a simple solution that works well. You declare Betting in the Player class like this:

void Betting(Table&);

And just to elaborate on this, will I want to declare these string variables as private data members and then access them through public member functions, or do I make them public from the get-go? I would make them const strings of course.

Is it also alright if I put these string variables in the Table class? I have two classes (NumberGuess and EvenOdd) which represent the two games you can play, and they are both derived from the Table class. Should I distribute the strings among those subclasses appropriately? If I did, I would have to declare the Betting function like so:

void Betting(NumberGuess&, EvenOdd&);

Which is preferred?
I wouldn't pass the whole table. Just pass the data the function is going to use (ie: just pass the strings)

perhaps:

void Player::Betting(const string& bettext, const string& oktext, const string& errortext)

Or if that's too "messy" you can throw all of that in a separate struct and pass the struct (although that's arguably even messier).

Passing the whole table is a bad idea though, because it makes Player have to assume too much about Table.

Keep classes as blind as possible about other classes. The more classes know about each other, the harder it is to expand and the harder maintanance becomes. (This is also one of the big reasons why you should avoid friendship where practical)
I think I'll make Betting a member function of Player.

"Messy" though it may look like, I think your suggestion of simply declaring a set of constant strings that I use is the best idea. I could put them in a separate file, perhaps? I could make use of the rand() function to let them - the imaginary dealers - alternate what they say.
Topic archived. No new replies allowed.