Code doesn't work - why?

Ali Çehreli via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Sep 16 22:16:23 PDT 2014


On 09/16/2014 09:08 PM, Robin wrote:

 > struct DeterministicState {

Structs are value types. When they are copied, the mutations that you 
expect may be happening on a copy.

I don't understand what exactly should happen but the following changes 
produce at least a different output. :)

1) Change the 'struct' above to 'class':

class DeterministicState {

2) Use the 'override' keyword with toString:

      override string toString() const {

3) Create the objects with new:

     auto s0 = new DeterministicState("s0", false);
     auto s1 = new DeterministicState("s1", false);
     auto s2 = new DeterministicState("s2", true);

Here is the output after that.

s0 has NO next for 0
s1 has NO next for 0
s0 has NO next for 0
s1 has NO next for 1
s0
s1
s0
s1
s2
Trace Length = 5

4) Also, the following conditional seems backward to me:

 >              if (currentState.hasNext(c) == false) {
 >                  writeln(currentState.toString() ~ " has next for " ~
 > to!string(c));

Should it not be simply 'if (currentState.hasNext(c))'?

Ali



More information about the Digitalmars-d-learn mailing list