[英]How to declare and initialize static constant strings in classes?
I'm writing a Yatzy program for c++ class. 我正在为c ++类编写Yatzy程序。 We are supposed to print the value of different dice using std::cout.
我们应该使用std :: cout打印不同骰子的值。 What I want to do is so save a constant string as then just that constant to print the dice out, so rather than using:
我想要做的是将一个常量字符串保存为该常量以打印出骰子,而不是使用:
std::cout << "-------\n| |\n| * |\n| |\n-------\n"
I want to have a constant string with that value and do this: 我想要一个带有该值的常量字符串,并执行以下操作:
std::cout << theConstantString;
Generic programming wins again! 通用编程又赢了!
-------
| |
| * |
| |
-------
The solution I have for it seems suboptimal to me. 我的解决方案对我来说似乎不是最佳的。 This is the relevant code:
这是相关代码:
YatzyIO.h YatzyIO.h
class YatzyIO
{
private:
// Define die constants
static const std::string dieOnePrint;
static const std::string dieTwoPrint;
static const std::string dieThreePrint;
static const std::string dieFourPrint;
static const std::string dieFivePrint;
static const std::string dieSixPrint;
void dieOne();
void dieTwo();
void dieThree();
void dieFour();
void dieFive();
void dieSix();
}; };
(there are more code than that in there, I just cut anything that wasn't relevent, which I assume I'm supposed to anyway) (那里的代码比那里的更多,我只是剪切了没有相关性的任何东西,我想我还是应该这样)
Now, outside the implementation of any function in YatzyIO.cpp: 现在,在YatzyIO.cpp中任何函数的实现之外:
const std::string YatzyIO::dieOnePrint = "-------\n| |\n| * |\n| |\n-------\n";
const std::string YatzyIO::dieTwoPrint = "-------\n| * |\n| |\n| * |\n-------\n";
const std::string YatzyIO::dieThreePrint = "-------\n| * |\n| * |\n| * |\n-------\n";
const std::string YatzyIO::dieFourPrint = "-------\n| * * |\n| |\n| * * |\n-------\n";
const std::string YatzyIO::dieFivePrint = "-------\n| * * |\n| * |\n| * * |\n-------\n";
const std::string YatzyIO::dieSixPrint = "-------\n| * * |\n| * * |\n| * * |\n-------\n";
And finally in YatzyIO.ccp I have these functions: 最后在YatzyIO.ccp中,我具有以下功能:
void YatzyIO::dieOne()
{
//output die side 1
std::cout << dieOnePrint;
}
(+1 for each die, and it looks similar) (每个骰子为+1,看起来相似)
This is only lab 2 of 3 I've finished so far, the third replaces these consts with arrays. 到目前为止,这只是我完成的3个实验中的2个,第三个用数组替换了这些const。 I was graded on lab 1, which contain this code as well and my teacher said (and I'm translating here since I'm swedish, so whatever is lost in translation I'm sorry!):
我在实验1中得到了评分,该实验也包含此代码,我的老师说(由于我是瑞典语,所以我正在这里进行翻译,所以翻译中丢失的内容对不起!):
"It's good that you use member variable so save the different dice output. I suggest you save the different lines (that form the different output) in your member variables instead." “最好使用成员变量,这样可以保存不同的骰子输出。建议您将不同的行(形成不同的输出)保存在成员变量中。”
What does he mean? 他什么意思? Is there a better way to do it?
有更好的方法吗? I can't initizalize non-integer constants in the header file.
我无法在头文件中初始化非整数常量。 I've tried a bunch of different way, because honestly my solution seems not so optimal to me.
我尝试了许多不同的方法,因为老实说,我的解决方案对我而言似乎并不是最佳选择。
Your teacher is probably suggesting that all different outputs are actually the combination of a small subset of lines, and that you could just maintain those lines and compose the solution as needed, rather than maintaining the whole dice image. 您的老师可能建议所有不同的输出实际上都是一小部分线的组合,您可以只维护这些线并根据需要编写解决方案,而不是维护整个骰子图像。
For example -------\\n
appears as the top and bottom borders for each and every different combination, | * * |\\n
例如,
-------\\n
出现在每种不同组合的顶部和底部边框, | * * |\\n
| * * |\\n
can be used to generate the top and bottom lines of 4, 5 and 6, and also the middle element of 6. | * |\\n
| * * |\\n
可以被用于产生的顶部和图4,图5和6的底线,也是6.中间元件| * |\\n
| * |\\n
can be the middle line for 1, 3, 5 and you will need an empty line for the top and bottom lines of 1, and for the middle lines in 2 and 4... You can store those as members and then generate the dice drawings based on those primitives. | * |\\n
可以是1、3、5的中间线,顶行和底线1以及2和4的中间线都需要一个空行。然后根据这些图元生成骰子图。
For example, to draw a 6 you need to do: 例如,要绘制6,您需要执行以下操作:
std::cout << line // -------
<< two_dots // | * * |
<< two_dots // | * * |
<< two_dots // | * * |
<< line; // -------
This way you only need to store the 4 primitives rather than all of the complete dice values. 这样,您只需要存储4个原语,而不是存储所有完整的骰子值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.