0

This question is a bit more deceiving than it looks so let me explain.


Suppose I have a class Tournament. One of the member variables is a vector of Player pointers.

class Tournament{
public:
 vector <shared_ptr<Player>> playersInTournament;
 vector <shared_ptr<Round>>  listOfRounds;
 //More code.

Suppose I also have a class called Round.

class Round{
public:
 vector <shared_ptr<Player>> playersInRound;
 Round(playersInTournament);
 //More code.

Round has a method called addPlayers(). When I add a player, I want to search the Tournament's playersInTournament vector to see if the player exists.


The problem:

I do not know how to directly refer to playersInTournament because I'm doing it from within the scope of a Round object.

Attempted/Proposed Solutions:

  1. Simply passing playersInTournament into the Round() constructor and creating a copy of it.

    I REALLY want to avoid this because it would be a lot of overhead when all I would be using it for is searching. In my head a pointer or a reference to playersInTournament could be more desirable because they'd take less memory once the list becomes big enough.

  2. Creating a reference as class member.

    My reasoning for this is Round has a compositional relationship to Tournament. The lifetime of Round will never exceed that of a Tournament object, and also Round does not "own" the vector, it is only meant to be manipulated by Tournament.

The problem I ran into immediately with that is references can't have a lazy initialization. I have nothing to directly reference it to until the round object itself is created, so that doesn't work for me.

And lastly,

  1. Pointer to vector as a class member.

    As far as I know, the only way to do this is to use vector.data() which returns pointer to the first element in the array used internally by the vector. This would allow me to iterate through the entire list for validation purposes which is okay, but my intention is to binary search the vector for a matching player and I don't know if I could do that using vector.data().

If there is something I'm missing, or any misconceptions I have please let me know. I could be wrong about using vector.data() but I'm just burnt out for the day and wanted a second opinion because this is a solo project I'm doing for fun.

Thank you for reading!

Best,

Julien Baker Was Right

  • Don't even stop for a millisecond to think about "a lot of overhead". It ain't gonna matter. Are you running the program a washing machine microcontroller? Or maybe your tournament has 1000000 players and you want to process 1000000 tournaments per second? No? It ain't gonna matter then. Copy the vector. Start thinking about negative impact of copying **when you start seeing negative impact of copying**. Not one minute before that. – n. m. could be an AI Jun 18 '20 at 06:49
  • 1
    Why should be checking whether players are in the tournament a responsibility of a round itself? Shouldn't this be a responsibility of someone who creates/defines a round instead? BTW, may the vector of players be modified during a tournament? – Daniel Langr Jun 18 '20 at 06:50
  • 1
    Your question seems to be a bit inconsistent for me. You say that references do not work for you because of you want to support lazy initialization. How then can copying vector in a constructor be a viable option? – Daniel Langr Jun 18 '20 at 06:56
  • 1
    What is the issue with simply passing a constant reference to the `playerInTournament` vector as an extra parameter in the `Round::addPlayer()` method (no need to copy nor to store it as an attribute) ? Since `Round` is part of `playerInTournament` (compositional relationship) you should always be able to provide this parameter during the lifetime of the `Round` instance. – Fareanor Jun 18 '20 at 07:06

1 Answers1

0

This is more like a design question. Let's take your question step by step.

Round has a method called addPlayers(). When I add a player, I want to search the Tournament's playersInTournament vector to see if the player exists.

You want to achieve: "Let Round check duplication when addPlayers()."

I do not know how to directly refer to playersInTournament because I'm doing it from within the scope of a Round object.

Then you found the problem: "How to let Round know how many players in a trounament?"

To answer your question, let's think about following questions.

  1. Should Round know every player in a trounament?
  2. Should Round check if addPlayers() has duplication?

My suggestion is both "no".

  1. A round should more like a POD, contains just the players in this round.
  2. There should be a round creator to handle which players should be in a round.
  3. A round creator should handle duplication, so addPlayer() would only be called once. Or never exists. Since you may pass all player in a round when constructing it.

Step back and look at what you want to achieve might save you from premature optimization.

Louis Go
  • 2,213
  • 2
  • 16
  • 29