www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Const again

reply naryl <cy ngs.ru> writes:
Is there a way to make something like this work in D 2.0?

clone is supposed to make a new shallow copy of it's object:

  interface Cloneable
  {
      const Cloneable clone();
  }

  class Demo : Cloneable
  {
      public :
          this(){}

          this( Demo reference )
          {
              reference_ =3D reference;
          }

          override const Demo clone() {
              return new Demo( reference_ );
          }
      private:
          Demo reference_;
  }

  void main()
  {
      auto a =3D new Demo(new Demo);
      auto b =3D a.clone();
  }

The method clone must be const and there must be a non-const constructor=
  =

in Demo.
Jan 24 2008
parent reply Jason House <jason.james.house gmail.com> writes:
naryl wrote:

 Is there a way to make something like this work in D 2.0?
 
 clone is supposed to make a new shallow copy of it's object:
 
   interface Cloneable
   {
       const Cloneable clone();
   }
 
   class Demo : Cloneable
   {
       public :
           this(){}
 
           this( Demo reference )
           {
               reference_ = reference;
           }
 
           override const Demo clone() {
               return new Demo( reference_ );
           }
       private:
           Demo reference_;
   }
 
   void main()
   {
       auto a = new Demo(new Demo);
       auto b = a.clone();
   }
 
 The method clone must be const and there must be a non-const constructor
 in Demo.
It looks like you should change this( Demo reference ) to this( const Demo reference ). Is that what you were saying is not allowed? If so, can you explain what's going wrong and why you can't?
Jan 27 2008
parent naryl <cy ngs.ru> writes:
On Sun, 27 Jan 2008 18:17:03 +0300, Jason House  =

<jason.james.house gmail.com> wrote:

 naryl wrote:

 Is there a way to make something like this work in D 2.0?

 clone is supposed to make a new shallow copy of it's object:

   interface Cloneable
   {
       const Cloneable clone();
   }

   class Demo : Cloneable
   {
       public :
           this(){}

           this( Demo reference )
           {
               reference_ =3D reference;
           }

           override const Demo clone() {
               return new Demo( reference_ );
           }
       private:
           Demo reference_;
   }

   void main()
   {
       auto a =3D new Demo(new Demo);
       auto b =3D a.clone();
   }

 The method clone must be const and there must be a non-const construc=
tor
 in Demo.
It looks like you should change this( Demo reference ) to this( const =
=
 Demo
 reference ).  Is that what you were saying is not allowed?  If so, can=
=
 you
 explain what's going wrong and why you can't?
Never mind. Shallow clone() cannot be const because reference_ may be = changed through it's copy. The problem was solved by making two versions= : = "Demo clone()" and "const Demo deepClone()": class Demo { public : this(){} public this(Demo reference) { reference_ =3D reference; } Demo clone() { if (reference_ !is null) return new Demo( reference_ ); else return new Demo(); } const Demo deepClone() { if (reference_ !is null) return new Demo( reference_.deepClone()= ); else return new Demo(); } private: Demo reference_; }
Jan 27 2008