Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to declare and initialize static constant strings in classes?
    text
    copied!<p>I'm writing a Yatzy program for c++ class. We are supposed to print the value of different dice using 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:</p> <pre><code>std::cout &lt;&lt; "-------\n| |\n| * |\n| |\n-------\n" </code></pre> <p>I want to have a constant string with that value and do this:</p> <pre><code>std::cout &lt;&lt; theConstantString; </code></pre> <p>Generic programming wins again!</p> <pre><code>------- | | | * | | | ------- </code></pre> <p>The solution I have for it seems suboptimal to me. This is the relevant code:</p> <p>YatzyIO.h</p> <pre><code>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(); </code></pre> <p>};<br> (there are more code than that in there, I just cut anything that wasn't relevent, which I assume I'm supposed to anyway)</p> <p>Now, outside the implementation of any function in YatzyIO.cpp:</p> <pre><code>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"; </code></pre> <p>And finally in YatzyIO.ccp I have these functions:</p> <pre><code>void YatzyIO::dieOne() { //output die side 1 std::cout &lt;&lt; dieOnePrint; } </code></pre> <p>(+1 for each die, and it looks similar)</p> <p>This is only lab 2 of 3 I've finished so far, the third replaces these consts with arrays. 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!):</p> <p>"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."</p> <p>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.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload