f**********t 发帖数: 1001 | 1 我想实现Card class里面的输入牌的名字
然后代码如下:
char* Card::NameOf() {
static char szName[20];
static char *Numbers[] = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "
10", "Jack", "Queen", "King"};
static char *Suits[] = {"Clubs", "Spades", "Hearts", "Diamonds"};
if (GetCardinal() < 13) {
strcat(szName, Numbers[GetCardinal()]);
}
strcat(szName, " of");
if (GetSuit() <= 4) {
strcat(szName, Suits[GetSuit() - 1]);
}
return szName;
}
reference在这里:http://msdn.microsoft.com/en-us/library/2dzy4k6e(v=vs.71).aspx
但是因为是static的char*,所以所有的串最后都会被连在一起输出:2 of clubs3 of
clubs4 of clubs....
但如果不是static,又不对。因为程序结束时空间都被收回了
所以这个函数咋整才能输出正确结果? |
|