Z
Zytan
Not sure what you call directly, but this...
Yes, right. const_cast does exist. But it could't be done on x
itself, only on val. But, it is pretty 'direct' in the case you
shown.
http://msdn2.microsoft.com/en-us/library/ms861539.aspx
"You cannot use the const_cast operator to directly override a
constant variable's constant status."
Yes. And, of course, there are other ways to do it. Get a pointer to
the same location, and dereference it. The real point is that 100%
prevention is not the issue. I think people think that because const
can be worked around its meaningless.
Ah. I wasn't thinking how valuable it could be enforced at run-time.
It's valuable enough as-is at compile time.
Yes, I was only speaking about the benefits of having C++ type const.
Nothing more.
I haven't claimed it is possible to ensure a const variable remains
unchanged. That has never been the point.
The point is the benefit it brings at compile time. My C++ experience
has shown me this.
Of course. I didn't say enforced constness was needed. I was
speaking only of the compile time errors that result.
This is the lack of understanding I fought through in the VB groups.
I don't know how to make it clear. const vars cannot be enforced
(unless the runtime does this), and I never claimed this is needed or
required. constness helps the compile stage tell you you're doing
something stupid, where you're giving something access to something it
should have access to, etc. That's where it helps. The fact that you
can cast away const is 100% irrelevant. It is sad that because C#
designers found they couldn't enforce const vars to be unchanging that
they didn't include it, since it's not required. A lock is still
useful even if some people can crack it.
Very true.
But, it's good to have an open mind about things and consider
possibilities.
Zytan
...
void sqrval(const int &val)
{
const_cast<int &> (val) = val * val; // cast away const on val}
int main()
{
int x = 22;
sqrval(x);
cout << "x after call: " << x << endl;
return 0;}
prints:
x after call: 484
... is pretty direct for me!
Yes, right. const_cast does exist. But it could't be done on x
itself, only on val. But, it is pretty 'direct' in the case you
shown.
http://msdn2.microsoft.com/en-us/library/ms861539.aspx
"You cannot use the const_cast operator to directly override a
constant variable's constant status."
Name it as you like, the const_cast operator is meant to cast-away const.
Yes. And, of course, there are other ways to do it. Get a pointer to
the same location, and dereference it. The real point is that 100%
prevention is not the issue. I think people think that because const
can be worked around its meaningless.
I didn't say it would be invaluable, it's simply not that valuable as it could be if it was
enforced by the run-time.
Ah. I wasn't thinking how valuable it could be enforced at run-time.
It's valuable enough as-is at compile time.
But as I said previously, the run-time can not enforce it because this would require all
major languages to support const, something which is not feasible as some languages pre-date
.NET.
Yes.
So what's left is support it at the language level, something which is done in C++/CLI, but
this offers nothing more than what you have in C++ , you got even less as the FCL which is
KEY in .NET doesn't supports const , and other .NET languages know nothing about const.
Yes, I was only speaking about the benefits of having C++ type const.
Nothing more.
see: because it's not enforceable on any parameter subsequently passed to a method (think
the FCL, written in C# and C++/CLI), it is not possible to insure the transitive closure on
the constness of the call chain for each parameter.
I haven't claimed it is possible to ensure a const variable remains
unchanged. That has never been the point.
The point is the benefit it brings at compile time. My C++ experience
has shown me this.
Yep, but as I said the CLI is a multilanguage platform, which is much more valuable than
non-enforced constness.
Of course. I didn't say enforced constness was needed. I was
speaking only of the compile time errors that result.
This is the lack of understanding I fought through in the VB groups.
I don't know how to make it clear. const vars cannot be enforced
(unless the runtime does this), and I never claimed this is needed or
required. constness helps the compile stage tell you you're doing
something stupid, where you're giving something access to something it
should have access to, etc. That's where it helps. The fact that you
can cast away const is 100% irrelevant. It is sad that because C#
designers found they couldn't enforce const vars to be unchanging that
they didn't include it, since it's not required. A lock is still
useful even if some people can crack it.
As I said, it's a can of worms, and IMO it makes no sense to open it any further as (IMO) we
won't have it anyway.
Very true.
But, it's good to have an open mind about things and consider
possibilities.
Zytan