template question

Started by
3 comments, last by Fruny 19 years, 6 months ago
can the paramaters of a func be an aray? this is for a card dealing function, card[51] is what needs to be shuffeled.
Advertisement
Pass it as a pointer.

void bar(){     card deck[] = new deck[52];     shuffle(deck, 52);}void shuffle(card* deck, int sizeOfArray){     deck[index] = 0;     // where index < sizeOfArray.}

..what we do will echo throughout eternity..
thanks
Quote:Original post by dan1088352
can the paramaters of a func be an aray? this is for a card dealing function, card[51] is what needs to be shuffeled.

Since you mention templating, you can actually save yourself from having to explicitly pass the size:

#include <cstddef>template< ::std::size_t ArrayLength >void shuffle( card (& deck)[ ArrayLength ] ){  // Do stuff}int main(){  card deck[51];  // ...  shuffle( deck );}


This, of course, will only work with an array whose size data is known at compile-time (IE usually a non-dynamically allocated array).
Quote:Original post by dan1088352
can the paramaters of a func be an aray? this is for a card dealing function, card[51] is what needs to be shuffeled.


You only have 51 cards? Ok.

#include <cstdlib>    // For srand()#include <ctime>      // For time()#include <algorithm>  // For std::random_shuffle()#include "card.h"     // Hypothetical header for your card typeint main(){   card deck[51];   srand(time(0));                      // initialize the RNG.   std::random_shuffle(card, card+51);  // shuffle}

"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

This topic is closed to new replies.

Advertisement