From -4608579854082530568
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f78e5,233d359668307510
X-Google-Attributes: gidf78e5,public
X-Google-ArrivalTime: 1995-03-16 06:19:21 PST
Path: bga.com!news.sprintlink.net!pipex!hursley.ibm.com!aixssc.uk.ibm.com!watnews.watson.ibm.com!locutus.rchland.ibm.com!powertool.rchland.ibm.com!pstaite
From: pstaite@powertool.rchland.ibm.com (Philip Staite)
Newsgroups: comp.std.c++
Subject: Re: A language feature or quirk ?
Date: 16 Mar 1995 14:19:21 GMT
Organization: IBM Rochester MN
Lines: 24
Distribution: world
Message-ID: <3k9hd9$11kf@locutus.rchland.ibm.com>
References: <3k2tn6$jig@mozo.cc.purdue.edu>
Reply-To: pstaite@vnet.ibm.com
NNTP-Posting-Host: powertool.rchland.ibm.com

In article <3k2tn6$jig@mozo.cc.purdue.edu>, kavuri@lips.ecn.purdue.edu (Surya N Kavuri ) writes:
|>  
|>  void Print(char *s) { cout << s; }
|>  
|> int main ()
|> {
|>   Print("Hello");  // compiles; not an error!
|>  
|>   const char* s = "Hello";
|>   Print(s); // Compile time error as expected!
|>   
|>   cout << "What on earth is going on ?" << endl;
|>   return 0;
|> }

Const correctness is going on :-)

To the compiler, "Hello" is a char*.  You can assign a char* to a const char*, as you do to s.  Now, s being a const char* you're not allowed to do anything that may change the chars s points to.

The Print function takes a char*, not a const char*.  If Print took a const char* it would promise not to change the chars pointed to by its argument.  However, Print makes no such promise.  Therefore the compiler cannot take the chance that Print will violate the constness of s.

-- 
Phil Staite
pstaite@vnet.ibm.com


