Why is it not possible to write to a file from a const member function ?

user42 via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Mar 12 06:02:31 PST 2016


Hi

I have the following snippet to illustrate my problem/question:

class X
{
   import std.stdio: write, File, stdout;

   private File* f = &stdout;

   void p(string s) const
   {
      f.write(s);
   }
}

class Y
{
   private string s = "Y";

   override string toString() const
   {
      return s;
   }
}

void main()
{
   auto x = new X;
   auto y = new Y;

   import std.conv: to;
   x.p(to!string(y));
}

Why is this thing not compiling ?
Or, in other words, how is is possible to log something to a file 
from a const member function ?

Thanks in advance


More information about the Digitalmars-d-learn mailing list