Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #70272

Re: I'm not able to find the error

From alexo <alelvb@inwind.it>
Newsgroups comp.lang.c++
Subject Re: I'm not able to find the error
Date 2019-12-05 03:58 +0100
Organization Aioe.org NNTP Server
Message-ID <qs9rph$195l$1@gioia.aioe.org> (permalink)
References <qs8ock$839$1@gioia.aioe.org> <qs90n8$1fld$1@gioia.aioe.org> <qs9jpt$7ah$1@gioia.aioe.org> <qs9r0h$162q$1@gioia.aioe.org>

Show all headers | View raw


Il 05/12/19 03:45, alexo ha scritto:
> Il 05/12/19 01:42, Manfred ha scritto:
>> On 12/4/19 8:16 PM, Manfred wrote:
>>>>
>>
>> Sorry, wrong line. Itis the /move/ assignment operator that should get 
>> a non-const reference.
>>


> So To summarize, if I have well understood, this should be the four 
> operators:
> 
>        Vector(const Vector& r) :              // the copy constructor
>            sz(r.size()),
>            data( new int[sz] )
>        {
>            for(size_t i=0; i < sz; i++)
>                data[i] = r.data[i];
>        }
> 
>        Vector(Vector&& r) :                   // the move constructor
>                sz{r.size()},
>                data{r.data}
>        {
>            r.sz = 0;
>            r.data = nullptr;
>        }
> 
>        Vector& operator= (const Vector& r)    // the copy assignment
>        {
>            if(this != &r)
>            {
>                delete [] data;
> 
>                sz = r.size();
>                data = new int[sz];
> 
>                for(size_t i=0; i < sz; i++)
>                    data[i] =  r.data[i];
>                }
> 
>            return *this;
>        }
> 
>        Vector& operator= (const Vector&& r)    // the move assignment
>        {
>            if(data)
>              delete [] data;
> 
>            data = r.data;
> 
>            return *this;
>        }
> 
> Thank you.

No, Manfred
That gives the runtime error again. Both the move compy constructor and 
the move assignment operator should take non const reference.

If I write them this way everything works:

       Vector(Vector&& r) :              // the move constructor
               sz{r.size()},
               data{r.data}
       {
           r.sz = 0;
           r.data = nullptr;
       }

       Vector& operator= (Vector&& r)    // the move assignment
       {
           if(data)
             delete [] data;

           data = r.data;
           r.data = nullptr;
           r.sz = 0;

           return *this;
       }

Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

I'm not able to find the error alexo <alelvb@inwind.it> - 2019-12-04 17:54 +0100
  Re: I'm not able to find the error Volker Wippel <Volker.Wippel@v2c2RemoveNoSpam.at> - 2019-12-04 18:40 +0100
  Re: I'm not able to find the error Daniel <danielaparker@gmail.com> - 2019-12-04 10:01 -0800
    Re: I'm not able to find the error Daniel <danielaparker@gmail.com> - 2019-12-04 10:06 -0800
  Re: I'm not able to find the error Manfred <noname@add.invalid> - 2019-12-04 20:16 +0100
    Re: I'm not able to find the error Manfred <invalid@add.invalid> - 2019-12-05 01:42 +0100
      Re: I'm not able to find the error alexo <alelvb@inwind.it> - 2019-12-05 03:45 +0100
        Re: I'm not able to find the error alexo <alelvb@inwind.it> - 2019-12-05 03:58 +0100
          Re: I'm not able to find the error Mike Terry <news.dead.person.stones@darjeeling.plus.com> - 2019-12-05 04:10 +0000
            Re: I'm not able to find the error alexo <alelvb@inwind.it> - 2019-12-05 05:45 +0100
          Re: I'm not able to find the error Manfred <noname@add.invalid> - 2019-12-05 17:29 +0100
  Re: I'm not able to find the error alexo <alelvb@inwind.it> - 2019-12-04 22:52 +0100
    Re: I'm not able to find the error Daniel <danielaparker@gmail.com> - 2019-12-04 16:47 -0800
      Re: I'm not able to find the error alexo <alelvb@inwind.it> - 2019-12-05 03:37 +0100

csiph-web