Re: Using ref

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



On Tue, 08 Jul 2008 07:42:10 -0700, Ben Voigt [C++ MVP] <rbv@xxxxxxxxxxxxx> wrote:

It doesn't prevent something from being modified in C++ either.

It does, unless the callee goes out of its way to break its contract (i.e.
uses the const_cast keyword to remove the const).

What does it matter _what_ the exception is? The fact is, the exception exists. As long as the exception exists, it's trivial for someone to modify something declared as "const" in this context.

More importantly, a much better way to ensure that a data structure
isn't modified is simply to make that data structure immutable. For
example, the String class is essentially "const" all the time. Works
great.

Not in C++ it isn't. No matter how immutable you make your object, C++ can
still overwrite the innards if you explicitly work to do so.

This isn't a C++ newsgroup. And presumably the OP would just use "const" if they were using C++. So forgive me for not going out of my way to qualify my statements explicitly so that everyone reading them knew they applied to C#, not C++. It never occurred to me that someone would be confused.

The lack of "const" for methods or their parameters doesn't make C#
any more flawed than any of the other languages people use on a daily
basis. It just makes it different from one of them.

If having "const" methods and parameters was such an important
feature, everyone would use C++ and no one would use C#, Java, VB,
etc. Odd, then, that so many people find languages other than C++
preferable, or at the very least, just as useful.

Or maybe it is something they really want, but they want garbage collection
and reflection more.

Like I said, "==>IF<== having 'const' methods and parameters ==>WAS SUCH AN IMPORTANT FEATURE<==...".

The question isn't whether people find it useful at all. It's whether it's important enough for someone to described a language without the feature as "deeply flawed".

It's just stupid to state or imply that "const" is a critical language feature. It obviously isn't, however useful it might be to some people.

Pete
.



Relevant Pages

  • Re: const
    ... the only person here who seems to use both languages ... It's clear enough from your original post and your reaction that you are not interested in "a rational _discussion_" about the issue, and frankly there's really no interest on my part for an argument per se. ... as far as me being an authority: I don't have the authority to state unequivocally that you'll never see "const" in C# the way you want it. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Does C# have static local variables like C++?
    ... void sqrval(const int &val) ... compiler checks that 'const' brings. ... 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. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: initonly array member as an lvalue
    ... But why would const or initonly be used on variables one wants to change later? ... My original point was that comparing two different keywords from two different languages was irrelevant, no matter how many articles or other people say they are the same.. ...
    (microsoft.public.dotnet.languages.vc)
  • Re: C or C++ do you prefer to program?
    ... which allows you to write more robust programs. ... > I actually like const, in particular when applied to a method (though ... > Dynamic cast is something every object oriented ... Dynamicly typed object oriented languages have no need for any ...
    (comp.unix.programmer)
  • Re: How to prevent passed value types from being changed
    ... proposals. ... taken at face value he was asking if a feature exists ... an array parameter, to allow different tables to be passed, you might ... formal parameter is const, the object referred to by the formal ...
    (microsoft.public.dotnet.languages.csharp)