🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c++
Article #2949

C++ const semantic

#2949
From: jlm
Date: Tue, 19 Dec 1995 02:50
86 lines
3236 bytes
| 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;}
};

| In other Object Oriented Languages, A::raz couldn't have been
| declared as a const methods.
|  
| - Why this "loose" C++ const semantic ?
| - What about implementing the "pure" const semantic ?
| - What about the following work-around ?

#define VAR 

template <class T>
class Ptr {
  private:
    T* ptr_;
  public:
    Ptr()      {ptr_ = 0; }
    Ptr(T* t)  {ptr_ = t;}
    ~Ptr()     {delete ptr_;}

    operator       T*   ()      VAR   { return  ptr_; }
    operator const T*   ()      const { return  ptr_; }
    VAR   T*  operator->()      VAR   { return  ptr_; }
    const T*  operator->()      const { return  ptr_; }
    VAR   T&  operator* ()      VAR   { return *ptr_; }
    const T&  operator* ()      const { return *ptr_; }
    VAR   T&  operator[](int i) VAR   { return  ptr_[i];}
    const T&  operator[](int i) const { return  ptr_[i];}
};

class A {
  public:
    void var_methode  () VAR   {}
    void const_methode() const {}
};

class B {
  public:
    B() : pure_ (new A[10]) {}
  private:
    Ptr<A> pure_;
    A*     loose_;
  public:
    void detect_viol_const    () const {pure_ ->var_methode  ();}
    void no_detect_viol_const () const {loose_->var_methode  ();}
    void const_methode        () const {pure_ ->const_methode();}
    void detect_viol_const2   () const {(*pure_) .var_methode  ();}
    void no_detect_viol_const2() const {(*loose_).var_methode  ();}
    void const_methode2       () const {(*pure_) .const_methode();}
    void detect_viol_const3   () const {((const A*)pure_ )->var_methode  ();}
    void detect_viol_const3b  () const {((A*)pure_ )->var_methode  ();}
    void no_detect_viol_const3() const {((A*)loose_)->var_methode  ();}
    void const_methode3       () const {((const A*)pure_ )->const_methode();}
    void detect_viol_const4   () const {pure_ [0].var_methode  ();}
    void no_detect_viol_const4() const {loose_[0].var_methode  ();}
    void const_methode4       () const {pure_ [0].const_methode();}
};

jlm

--
+----------------------------+------------------------------------+
|   Jean-Louis Moser         |    2001 SA                         |
|   tel: 33 (1)46.66.54.54   |    2, rue de la renaissance        |
|   Email: jlm@two-oo-one.fr |    F-92184 Antony Cedex            |
+----------------------------+------------------------------------+






---
[ 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: <9512181324.AA08686@ludo.two-oo-one.fr>
Path: arc1-ca.pugleaf.net!howland.reston.ans.net!agate!agate!matt

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!howland.reston.ans.net!agate!agate!matt
From: jlm@two-oo-one.fr
Newsgroups: comp.std.c++
Subject: C++ const semantic
Date: 19 Dec 1995 02:50:24 GMT
Organization: <none>
Approved: matt@Austern (Matt Austern)
Message-ID: <9512181324.AA08686@ludo.two-oo-one.fr>
NNTP-Posting-Host: austern.hip.berkeley.edu