C++: Array assignment problem

Discussion in 'C++' started by Newsnet Customer, Aug 25, 2003.

  1. <snippet>
    ....

    // overloads the = operator to allow array assignment.
    MyArray& MyArray::eek:perator=(const MyArray& old)
    {
    size = array.size;
    ia = new (std::nothrow)int[size];

    for (int i=0; i<size; i++)
    {
    ia = old; //***error indicated here
    }

    return *this;
    }

    //end

    ***error:
    passing `const Array2' as `this' argument of `int & Array2::eek:perator
    [](int)' discards qualifiers
    Newsnet Customer, Aug 25, 2003
    #1
    1. Advertising

  2. Re: Array assignment problem

    "Newsnet Customer" <> wrote in message
    news:...
    > <snippet>
    > ...
    >
    > // overloads the = operator to allow array assignment.
    > MyArray& MyArray::eek:perator=(const MyArray& old)
    > {
    > size = array.size;
    > ia = new (std::nothrow)int[size];
    >
    > for (int i=0; i<size; i++)
    > {
    > ia = old; //***error indicated here
    > }
    >
    > return *this;
    > }
    >
    > //end
    >
    > ***error:
    > passing `const Array2' as `this' argument of `int & Array2::eek:perator
    > [](int)' discards qualifiers
    >


    You need to declare two versions of operator[], one for const arrays (like
    old) and one for non-const arrays.

    class MyArray
    {
    public:
    int& operator[](int i);
    int operator[](int i) const;
    };

    At the moment you are trying to use a non-const operator[] on a const array.

    john
    John Harrison, Aug 25, 2003
    #2
    1. Advertising

  3. Newsnet Customer wrote:
    >
    > <snippet>
    > ...
    >
    > // overloads the = operator to allow array assignment.
    > MyArray& MyArray::eek:perator=(const MyArray& old)
    > {
    > size = array.size;
    > ia = new (std::nothrow)int[size];
    >
    > for (int i=0; i<size; i++)
    > {
    > ia = old; //***error indicated here
    > }
    >
    > return *this;
    > }
    >
    > //end
    >
    > ***error:
    > passing `const Array2' as `this' argument of `int & Array2::eek:perator
    > [](int)' discards qualifiers


    Did you provide an operator[].
    If yes, you probably need a second version which is marked const.

    int MyArray::eek:perator[]( int Index ) const;
    *****


    --
    Karl Heinz Buchegger
    Karl Heinz Buchegger, Aug 25, 2003
    #3
  4. Newsnet Customer

    ES Kim Guest

    Re: Array assignment problem

    "Newsnet Customer" <> wrote in message
    news:...
    > <snippet>
    > ...
    >
    > // overloads the = operator to allow array assignment.
    > MyArray& MyArray::eek:perator=(const MyArray& old)
    > {
    > size = array.size;
    > ia = new (std::nothrow)int[size];
    >
    > for (int i=0; i<size; i++)
    > {
    > ia = old; //***error indicated here
    > }
    >
    > return *this;
    > }
    >
    > //end
    >
    > ***error:
    > passing `const Array2' as `this' argument of `int & Array2::eek:perator
    > [](int)' discards qualifiers


    No additional explanation is not necessary now that JH and KHB have
    provided the solution.
    Other points are:
    It is necessary to take care of self-assignment.
    Before new, the memory 'ia' points to should be released.

    BTW, what does "new (std::nothrow)" mean? I've seen nothrow used with
    a function declaration, but not with new.

    --
    ES Kim
    ES Kim, Aug 25, 2003
    #4
  5. Re: Array assignment problem

    >
    > BTW, what does "new (std::nothrow)" mean? I've seen nothrow used with
    > a function declaration, but not with new.
    >


    It calls a version of new that returns NULL when out of memory (instead of
    throwing an exception). No obvious reason why the OP is using it however.

    john
    John Harrison, Aug 25, 2003
    #5
  6. Re: Array assignment problem

    John Harrison wrote:
    >
    > >
    > > BTW, what does "new (std::nothrow)" mean? I've seen nothrow used with
    > > a function declaration, but not with new.
    > >

    >
    > It calls a version of new that returns NULL when out of memory (instead of
    > throwing an exception). No obvious reason why the OP is using it however.


    :) Especially if he doesn't check the return value :)

    But: often code snippets posted are just excerpts of the real
    code, so I assume that the OP does some error checking in his
    real code. That wouldn't fix the self assignment bug, of course
    (which would go away, if the operator is written in a completely
    exception safe way).

    --
    Karl Heinz Buchegger
    Karl Heinz Buchegger, Aug 25, 2003
    #6
  7. Newsnet Customer

    ES Kim Guest

    Re: Array assignment problem

    "John Harrison" <> wrote in message
    news:bid7eq$860be$-berlin.de...
    > >
    > > BTW, what does "new (std::nothrow)" mean? I've seen nothrow used with
    > > a function declaration, but not with new.
    > >

    >
    > It calls a version of new that returns NULL when out of memory (instead of
    > throwing an exception). No obvious reason why the OP is using it however.
    >
    > john


    Oh, I see. Is it specified in the standard or an extension of a compiler?
    AFAIK, new throws bad_alloc if failed.

    --
    ES Kim
    ES Kim, Aug 25, 2003
    #7
  8. Re: Array assignment problem

    ES Kim wrote:
    >
    > "John Harrison" <> wrote in message
    > news:bid7eq$860be$-berlin.de...
    > > >
    > > > BTW, what does "new (std::nothrow)" mean? I've seen nothrow used with
    > > > a function declaration, but not with new.
    > > >

    > >
    > > It calls a version of new that returns NULL when out of memory (instead of
    > > throwing an exception). No obvious reason why the OP is using it however.
    > >
    > > john

    >
    > Oh, I see. Is it specified in the standard


    It is standard

    > or an extension of a compiler?
    > AFAIK, new throws bad_alloc if failed.
    >


    --
    Karl Heinz Buchegger
    Karl Heinz Buchegger, Aug 25, 2003
    #8
  9. Re: Array assignment problem

    ES Kim wrote:

    > "John Harrison" <> wrote in message
    > news:bid7eq$860be$-berlin.de...
    >
    >>>BTW, what does "new (std::nothrow)" mean? I've seen nothrow used with
    >>>a function declaration, but not with new.
    >>>

    >>
    >>It calls a version of new that returns NULL when out of memory (instead of
    >>throwing an exception). No obvious reason why the OP is using it however.
    >>
    >>john

    >
    >
    > Oh, I see. Is it specified in the standard or an extension of a compiler?
    > AFAIK, new throws bad_alloc if failed.
    >


    It is standard.

    -Kevin
    --
    My email address is valid, but changes periodically.
    To contact me please use the address from a recent posting.
    Kevin Goodsell, Aug 25, 2003
    #9
  10. Re: Array assignment problem

    "John Harrison" <> wrote in message
    news:bid4et$79pni$-berlin.de...
    >
    > "Newsnet Customer" <> wrote in message
    > news:...
    > > <snippet>
    > > ...
    > >
    > > // overloads the = operator to allow array assignment.
    > > MyArray& MyArray::eek:perator=(const MyArray& old)
    > > {
    > > size = array.size;
    > > ia = new (std::nothrow)int[size];
    > >
    > > for (int i=0; i<size; i++)
    > > {
    > > ia = old; //***error indicated here
    > > }
    > >
    > > return *this;
    > > }
    > >
    > > //end
    > >
    > > ***error:
    > > passing `const Array2' as `this' argument of `int & Array2::eek:perator
    > > [](int)' discards qualifiers
    > >

    >
    > You need to declare two versions of operator[], one for const arrays (like
    > old) and one for non-const arrays.
    >
    > class MyArray
    > {
    > public:
    > int& operator[](int i);
    > int operator[](int i) const;
    > };
    >
    > At the moment you are trying to use a non-const operator[] on a const

    array.
    >
    > john


    Well apparently it had nothing to do with operator[].

    ia = old; //***error indicated here

    should have been:

    ia = old.ia;

    I guess it's hard to see without the full code despite my attempt with the
    code snippet.

    Regards
    dsf
    Newsnet Customer, Aug 26, 2003
    #10
  11. Re: Array assignment problem

    Newsnet Customer wrote:
    >
    > "John Harrison" <> wrote in message
    > news:bid4et$79pni$-berlin.de...
    > >
    > > "Newsnet Customer" <> wrote in message
    > > news:...
    > > > <snippet>
    > > > ...
    > > >
    > > > // overloads the = operator to allow array assignment.
    > > > MyArray& MyArray::eek:perator=(const MyArray& old)
    > > > {
    > > > size = array.size;
    > > > ia = new (std::nothrow)int[size];
    > > >
    > > > for (int i=0; i<size; i++)
    > > > {
    > > > ia = old; //***error indicated here
    > > > }
    > > >
    > > > return *this;
    > > > }
    > > >
    > > > //end
    > > >
    > > > ***error:
    > > > passing `const Array2' as `this' argument of `int & Array2::eek:perator
    > > > [](int)' discards qualifiers
    > > >

    > >
    > > You need to declare two versions of operator[], one for const arrays (like
    > > old) and one for non-const arrays.
    > >
    > > class MyArray
    > > {
    > > public:
    > > int& operator[](int i);
    > > int operator[](int i) const;
    > > };
    > >
    > > At the moment you are trying to use a non-const operator[] on a const

    > array.
    > >
    > > john

    >
    > Well apparently it had nothing to do with operator[].


    Apparently it had everything to do with operator[], as this is
    what the compiler complains about.

    >
    > ia = old; //***error indicated here
    >
    > should have been:
    >
    > ia = old.ia;


    Did you try the suggested fixes?
    They would have worked also, with the added benefit, that the fix will
    also enable you to use operator[] on const objects in functions which don't
    have access to the internals of MyArray.

    --
    Karl Heinz Buchegger
    Karl Heinz Buchegger, Aug 26, 2003
    #11
  12. Re: Array assignment problem


    > Newsnet Customer wrote:
    > >
    > > > Try this
    > > >
    > > > const MyArray carray( whatever ); // replace whatever with ...

    whatever
    > > > cout << carray[0];
    > > >
    > > > Should that compile? Of course it should, but I'm willing to bet it

    > > doesn't
    > > > for you.
    > > >
    > > > Your fix is fine, but you still need the improvement suggested.

    > >
    > > Tried it, but it didn't like carray[0];

    >
    > For the very same reason your original problem didn't compile.
    > And that's what John (and I) wanted to bring across (but
    > we obviously failed):
    > With your fix, you made the operator= workable. You fixed s single
    > problem.
    > With the fix suggested by us, you would fix a whole family of
    > problems (your original, the one above and many more, whenever
    > there is a const MyArray involved).


    I understand that the operator[] does not work with the MyArray data type I
    have created., and that I have to overload it to get it to do what I want. I
    just haven't done that yet - too lazy.

    Thanks for your help and John if hes still listening.

    sasasa
    Newsnet Customer, Aug 27, 2003
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. nagy
    Replies:
    36
    Views:
    986
    Terry Reedy
    Jul 20, 2006
  2. Chris
    Replies:
    34
    Views:
    1,493
  3. Myth__Buster
    Replies:
    13
    Views:
    356
    Anand Hariharan
    Feb 26, 2013
  4. Myth__Buster
    Replies:
    0
    Views:
    237
    Myth__Buster
    Feb 1, 2013
  5. Myth__Buster
    Replies:
    1
    Views:
    209
    Myth__Buster
    Feb 1, 2013
Loading...

Share This Page