assigmnet operator when class data members are immutable


C

chuck

Hello,
I have a class whose data members are constant. So, default
assignment will not happen. I am trying to write and assignment
operator method that will allow assignment.

I think my assignment with the swap might be the right direction but I
am currrently stumped.
Can anyone help with this assignment operator?

Thanks,
Chuck

incase formating is messed up
http://pastebin.ca/962759

#include <cassert> // assert
#include <iostream> // cout, endl
#include <vector> // vector
#include <algorithm>

struct A {
const int i;

A (int i) : i (i)
{}

A operator = (A other){
A temp = A(other);
//std::cout << "temp = " << temp << std::endl;
return temp;
}

/*
A& operator = (A other){
A temp = A(other);
//std::cout << "temp = " << temp << std::endl;
std::swap(*this, temp);
return *this;
}
*/

friend std::eek:stream& operator << (std::eek:stream& lhs, const A& rhs)
{ lhs << rhs.i;
return lhs;
}
};

int main () {
using namespace std;
A x(2);
A w(3);
vector<A> y(10, x);
vector<A> z(5,w);
z[2]=y[4];

//2 2 2 2 2 2 2 2 2 2
for(size_t i = 0; i < 10; ++i)
{ cout << y << " ";
}
cout << endl;

//Problem - should be
//3 3 2 3 3
for(size_t i = 0; i < 5; ++i)
{ cout << z << " ";
}
cout << endl;
}
 
Ad

Advertisements

J

James Kanze

I have a class whose data members are constant. So, default
assignment will not happen. I am trying to write and
assignment operator method that will allow assignment.

You can't change const members once they've been initialized.
If assignment should change them, then don't make them const.
I think my assignment with the swap might be the right
direction but I am currrently stumped.

You can't swap a const member.
Can anyone help with this assignment operator?
#include <cassert> // assert
#include <iostream> // cout, endl
#include <vector> // vector
#include <algorithm>

struct A {
const int i;

[...]
A& operator = (A other){
A temp = A(other);

There's an extra copy here. Normally, operator= takes its
argument as a reference to const. That's what the compiler
does when it generates one, and anything else should only be
used in exceptional cases.
//std::cout << "temp = " << temp << std::endl;
std::swap(*this, temp);

And this, of course, will result in infinite recursion. You
can't implement the operator= of a class in terms of std::swap
on that class. You have to provide a member function swap, and
use it.

Of course, in your case, you can't provide a member function
swap, because you cannot swap const int. In practice, too, the
swap idiom really isn't needed, or even appropriate, for classes
which don't have members of pointer or class type. If your
class only contains int's, enum's and double's, then using the
swap idiom only introduces a lot of overhead for nothing.
return *this;
} [...]
};
 
Ad

Advertisements

J

Juha Nieminen

chuck said:
Hello,
I have a class whose data members are constant. So, default assignment
will not happen.

Well, if you made it const that means you don't want assignment to
happen in the first place. The situation is not any different from this:

const int i = 5;
i = 8; // Hey, I want to assign to a const, but can't.

If you want to support assignment, don't make it const.

(OTOH, even having const member functions doesn't stop you from
writing an assignment operator function for the class. This function
just cannot modify the const member variables, only the non-const ones.
If these const variables have different values in the source and target,
then the assignment is dubious, of course, because the objects will not
be equal after an assignment, which may in some cases break something.
Of course as long as they *compare* equal, ie. they don't compare the
const members, then it may be ok. Still a bit dubious, though.)
 

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

Top