• 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
Const correctness
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Const correctness


  • Subject: Const correctness
  • From: "Theodore H. Smith" <email@hidden>
  • Date: Mon, 6 Jun 2005 17:20:44 +0100

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. But there could be more. Anytime that you convert a non-const data to a const data param, if that const data is then saved after the function is completed, then should the non-const original data be altered, the saved const pointer is now screwed.


I generally think, that if you want immutable data, it should be in a separate class/type from the mutable data, and only have very few (ideally one), way to get the immutable data from the mutable data.

I've done this with some classes I've designed. I have one mutable class which has a ton of data input functions. I have another immutable class with a ton of data analysis functions.

It works...

In fact, I think it works better than Apple's NS<Type>, and NSMutable<Type> for some cases. This is because, there is no NSImmutable<Type> kind of class. You can't define at compile time for example, that your NSDictionary* might not actually be an NSMutableDictionary* , and so if some class handed to you a mutable version and then later altered it, you'd once again get bugs.

--
http://elfdata.com/plugin/ Industrial strength string processing, made easy.


"All things are logical. Putting free-will in the slot for premises in
a logical system, makes all of life both understandable, and free."

_______________________________________________
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: Cameron Hayne <email@hidden>
  • Prev by Date: Optimization report in xcode??
  • Next by Date: stl documentation
  • Previous by thread: Optimization report in xcode??
  • Next by thread: Re: Const correctness
  • Index(es):
    • Date
    • Thread