On Tue, 30 Nov 2010 16:05:16 -0500, Walter Bright <newshou...@digitalmars.com> wrote:

Steven Schveighoffer wrote:
On Tue, 30 Nov 2010 15:16:04 -0500, Walter Bright <newshou...@digitalmars.com> wrote:

Steven Schveighoffer wrote:
The example that I gave does not seem to you like it would surprise someone? I passed in a const object and it got modified, even though no casts were used.

No, it doesn't surprise me. Const on one object does not apply to another object.
 So this:
 void myfn(const(C) n)
{
   assert(n.x == 1);
   n.foo();
   assert(n.x == 5);
}
 Wouldn't be surprising to you?

No. There are a lot of things a member "x" could be. The const only applies to the instance data.

If you read my previous code, x is instance data. I'll reiterate the class definition:

class C
{
   static C theCommonOne;
   int x;
   void foo() const {
      theCommonOne.x = 5; // theCommonOne could be this
   }
}

If you find the above unsurprising, you are in the minority. I find it surprising, and invalid that anyone would write code this way. People simply just don't do that normally. It's just written to demonstrate a point that the compiler does not guarantee anything via const, it's guaranteed by convention. The compiler simply helps you follow the convention.

-Steve

Reply via email to