Article View: comp.std.c++
Article #3006Re: C++ const semantic
From: D. Allan Drummond
Date: Thu, 11 Jan 1996 15:36
Date: Thu, 11 Jan 1996 15:36
43 lines
1846 bytes
1846 bytes
jlm@two-oo-one.fr wrote:
>
> In C++ the "A::raz () const" doesn't change the content
> of an A, but it may change its behaviour. Let me take a
> simple example:
>
> class A {
> char* text_;
> public:
> A (const char* t)
> : text_ (strcpy (new char[strlen(t)+1], t)) {}
> void raz () const {text_[0] = 0;}
> };
While it's true that the behavior of an A may change, all that
const means is that its content does not change -- which you
appear to recognize.
The problem in this case is that you are using char* to mean
"string", when all it really means is "pointer to char". A
better workaround, using the new standard (STL and ANSI string),
is to say:
#include <bstring.h> // HP public-domain string implementation
class A {
string text_;
public:
A( const char* t ) : text_( t ) {}
void raz() { text_[0] = 0; }
};
Note that raz() now cannot be const - making it const produces a
compile-time error.
Allan
allan.drummond@trilogy.com
---
[ comp.std.c++ is moderated. Submission address: std-c++@ncar.ucar.edu.
Contact address: std-c++-request@ncar.ucar.edu. The moderation policy
is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
Technical Details
Message-ID:
<30F4C3D2.112E@trilogy.com>Path:
arc1-ca.pugleaf.net!in1.uu.net!bounce-backReferences:
<9512181324.AA08686@ludo.two-oo-one.fr>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!in1.uu.net!bounce-back Date: 11 Jan 96 15:36:49 GMT Approved: fjh@cs.mu.oz.au From: "D. Allan Drummond" <allan.drummond@trilogy.com> Newsgroups: comp.std.c++ Subject: Re: C++ const semantic X-Original-Date: Thu, 11 Jan 1996 02:01:22 -0600 Organization: Trilogy Message-ID: <30F4C3D2.112E@trilogy.com> References: <9512181324.AA08686@ludo.two-oo-one.fr> X-Mailer: Mozilla 2.0b3 (WinNT; I) Cc: std-c++@ncar.ucar.edu, @smtp@trilogy.com X-Auth: PGPMoose V1.1 PGP comp.std.c++ iQBFAgUBMPUunuEDnX0m9pzZAQGicwGAl0FUcKqdealWYECPb/6QxGBIw5wlz/M/ QT9aKZtpHtJpiLy/Kqs+7r0/NbDgqW8s =dAO4