a form of nested function calls

So I realize this may have been answered in another posting (if so please link it to me so no one has to copy/paste the whole response), but I'm wondering if C++ allows for a class to have a function that calls the function of another class (that it is friends with) that in turn calls a function in a third class (again they are friends). To explain what I want I have a class called hand that has a function that calls a function called createCard in the class of Card, this in turn calls the function drawCard in the class of Deck. Anyone have an idea as to how to approach this?
Hmmm....I don't see a problem with doing that.

Well, of course the various functions need to have access to a instance of the objects that its calling a function from (hopefully the same instance, as you don't want a new deck to be created each time). Other then that I don't see why you would have a problem with that.
I would avoid using the keyword friend in this case and just focus on good design.

a function that calls a function called createCard in the class of Card


You are over-complicating things.

Try this:

1
2
3
4
5
6
7
8
enum Suit { CLUBS, HEARTS, DIAMONDS, SPADES };
enum Rank { ONE = 1, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING, ACE };

struct Card 
{
	Suit suit;	
	Rank rank;
};


In your Deck class you can create a full deck using a nested for loop:

1
2
3
4
5
6
7
8
9
10
                Card tmpCard;
		
                for (int i = CLUBS; i < SPADES; i++)
			for (int j = ONE; j <= ACE; j++)
			{				
				tmpCard.suit = (Suit)i;
				tmpCard.rank = (Rank)j;			
				deck.push_back(tmpCard);
			} 
                 



Now you have a full deck and can add methods like Shuffle, DrawCard, PutBackTop, PutBackBottom, Deal(int numOfPlayers), etc.


If you create a Hand class it would just interact with the Deck class' public interface. An alternative design would be to create a Payer class that contains a vector or list of cards.

You shouldn't have any problem calling class methods within other classes. As long as you understand scope and access levels (public, protected, private) then you should be fine.



ICE the problem with that idea is that Im using this for a blackjack game so Im just using a vector of ints for the deck pulling one of them out with a rand function then removing that storage passing the number to class card which then assigns it a rank a suit and a value (since all face cards have a value of 10). So I wanna call a function from the players hand that sends a request to the card class that in turn request an int from the deck that int gets passed to the card class which assigns its info then passes that to the appropiate hand. Im starting to think the only reliable way would be to use a few function calls directly call the deck from the hand get the int then pass it to card which would return the correct card.
So I wanna call a function from the players hand that sends a request to the card class that in turn request an int from the deck that int gets passed to the card class which assigns its info then passes that to the appropiate hand.


The hand shouldn't have to ask for a card. There should be a Dealer that takes a Card from the Deck and places it in a Hand.
Topic archived. No new replies allowed.