• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Const correctness
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Const correctness


  • Subject: Re: Const correctness
  • From: Keith Ray <email@hidden>
  • Date: Wed, 8 Jun 2005 15:27:02 -0700

When writing your own classes, designing them to be immutable can be
very useful - particularly for "value" types. [see "Domain Driven
Design" by Eric Evans for the distinctions between "value" types and
"entity" types.]

On 6/6/05, Cameron Hayne <email@hidden> wrote:
> On 6-Jun-05, at 12:20 PM, Theodore H. Smith wrote:
>
> > I just realised, that C++'s upgrading of normal data, to const
> > data, actually defeats the entire purpose of having consts in the
> > first place.
> >
> >     char* noConstsAreBetter = "these consts suck\n";
> >     const char* constsSuck = noConstsAreBetter;
> >
> >     printf( constsSuck );
> >     noConstsAreBetter[0] = 'T'; // either constsSuck is altered, or
> > your app will crash.
> >     printf( constsSuck );
> >
> >
> > That's one example of how you can alter const data, with perfectly
> > legal source code, that the compiler will not complain about.
>
> I think you have misunderstood the purpose of 'const'. It does not
> say that the data is invariant. It merely says that the variable
> declared as 'const' cannot be used to change the data. It is of
> course quite possible to change the data by using other, non-const
> variables.
>
> And the runtime crash in the above example has absolutely nothing to
> do with 'const' and everything to do with trying to change an
> embedded string.
> I.e. the following crashes exactly the same:
>      char* noConstsAreBetter = "these consts suck\n";
>      printf( noConstsAreBetter );
>      noConstsAreBetter[0] = 'T';
>      printf( noConstsAreBetter );
>
> --
> Cameron Hayne
> email@hidden
>
>
>  _______________________________________________
> Do not post admin requests to the list. They will be ignored.
> Xcode-users mailing list      (email@hidden)
> Help/Unsubscribe/Update your Subscription:
>
> This email sent to email@hidden
>


--

C. Keith Ray
<http://homepage.mac.com/keithray/blog/index.html>
<http://homepage.mac.com/keithray/xpminifaq.html>
<http://homepage.mac.com/keithray/resume2.html>
 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Re: Const correctness
      • From: Markian Hlynka <email@hidden>
References: 
 >Const correctness (From: "Theodore H. Smith" <email@hidden>)
 >Re: Const correctness (From: Cameron Hayne <email@hidden>)

  • Prev by Date: Re: NSInstallLinkEditErrorHandlers() does not work in Tiger.
  • Next by Date: Re: Xcode 2.1 debugger grief
  • Previous by thread: Re: Const correctness
  • Next by thread: Re: Const correctness
  • Index(es):
    • Date
    • Thread