delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/09/07/02:55:52

From: rjr AT mit DOT edu (Robert J. Ragno)
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Annoying: const char * in Allegro
Date: Tue, 07 Sep 1999 03:44:39 GMT
Organization: Massachvsetts Institvte of Technology
Lines: 42
Message-ID: <37d46401.7392519@news.mit.edu>
References: <8D53104ECD0CD211AF4000A0C9D60AE30162E5F0 AT probe-2 DOT acclaim-euro DOT net>
NNTP-Posting-Host: righteous-indignation.mit.edu
Mime-Version: 1.0
X-Newsreader: Forte Agent 1.5/32.452
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

On Mon, 6 Sep 1999 13:05:00 +0100, Shawn Hargreaves
<ShawnH AT Probe DOT co DOT uk> wrote:

>Damian Yerrick writes:
>> All of Allegro's string-handling (textout(), textprintf(),
>> files, etc.) functions take a char * instead of a const
>> char *. Anyone know why?
>
>No particular reason, except that I've never particularly grokked
>things being const, don't tend to use it myself, and hence never used
>it in Allegro either.

Using const in paremeters in declarations is a Really Good Thing,
IMHO, and it's viral nature is an asset.

It can be a help to optimization, but that is not the main reason to
do it.

Basically, making a parameter const says that you will not change it.
(though I'm sure everyone knows this).  It is a nice semantic hint to
people using your function. The only parameters I don't tend to const
are ones that are used to return values. "const int * a" does not say
that 'a' cannot be changed -- it says that the value pointed to by 'a'
cannot be changed. (you can get the first effect by saying "int *
const a").

(In gripjoy.c, you'll see a function prototyped as:
static char * new_text(const char * const txt);  :)

const also allows the compiler to enforce this at compile time and to
catch bugs you could have easily overlooked. It lets you breathe
easier when you use other people's code, as you can be assured that
they do not mess with things they shouldn't.

A problem occurs if other people you are working with don't realize
the benefits of using const. You must then cast all your variables to
non-const before passing them to functions that take non-const things.
That's messy, and you lose the security of knowing that things are
modified only where they are supposed to be modified.

 - Rob

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019