🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

2 total messages Started by jlm Tue, 19 Dec 1995 02:50
C++ const semantic
#2949
Author: 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. ]
Re: C++ const semantic
#3006
Author: D. Allan Drummond
Date: Thu, 11 Jan 1996 15:36
43 lines
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. ]
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads