C++: Array assignment problem

N

Newsnet Customer

<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
 
J

John Harrison

Newsnet Customer said:
<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
 
K

Karl Heinz Buchegger

Newsnet said:
<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;
*****
 
E

ES Kim

Newsnet Customer said:
<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.
 
J

John Harrison

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
 
K

Karl Heinz Buchegger

John said:
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).
 
E

ES Kim

John Harrison said:
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.
 
N

Newsnet Customer

John Harrison said:
Newsnet Customer said:
<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
 
K

Karl Heinz Buchegger

Newsnet said:
John Harrison said:
Newsnet Customer said:
<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.
 
N

Newsnet Customer

Newsnet said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top