I agree that my_deck is a Deck. However, the Deck consists of a list<Card> doesn't it? |
That doesn't matter. The dot operator works like this:
object.Func();
'object' is an instance of some class (in this case, Deck). The above code tells to compiler to look in Deck for a function called "Func" and call it.
It does not look at each member of Deck to see if those objects have a Func. Doing that wouldn't make any sense and would be problematic anyway.
I used this exact same line in the Deck class (list<Card>::iterator it = my_deck.begin(); |
In that situation, 'my_deck' must have been a list. In which case it works because list has a member function called 'begin'
Ultimately, I am trying to pass a Deck of Cards to the Player function to allow the Player to draw cards from the Deck. Will what I am doing work (with some changes)? Or do I need to approach this differently? |
I would approach it differently. Outside classes/code should not know or care whether or not Deck contains a list. Deck should be the only part of your program that knows how it works.
You should create member functions that the rest of your program to use to work with the Deck.
In this case, it would make sense to have a "Draw" member function:
1 2
|
// in your Deck class:
Card Draw();
| |
Then when the player needs to draw some cards:
1 2 3 4 5 6 7
|
void Player::DrawAHand(Deck& deck)
{
// draw a hand of 5 cards
// assume 'my_hand' is a vector<Card>
for(i = 0; i < 5; ++i)
my_hand.push_back( deck.Draw() );
}
| |
Once you get the idea of how to write code this way, it makes it a lot easier to read and understand. Without even seeing the Deck class or Draw function, you already know what it does and how to use it.
EDIT: revised the DrawAHand example to make more sense