Reputation: 37
I have an assignment in which I have to write a simple class. This class must hold an array of strings, and contain an overload of the '+' operator, which combines the elements of two arrays into a new array and returns it.
Additionally, this function must be 'const', which is where I ran into problems. When trying to change the class "size" attribute and the array it is holding, I get errors. I also get an error when trying to return the object. I understand the cause for the first two errors is because I have declared the function 'const', but my question is, what is the proper way to reassign these values inside of a const method?
Don't tear into me too bad, I've just begun learning C++, and the concepts are a bit new to me. I've tried researching the subject, but I haven't found any useful answers. That being said, I really need to get this function working, so any help will be much appreciated.
Here is the class:
class FirstClass{
private:
string* sList;
unsigned int _size;
public:
FirstClass(const unsigned int initSize = 1);
// copy, assignment, and destructor
FirstClass& operator+(const FirstClass& add)const;
};
FirstClass::FirstClass(const unsigned int initSize):_size(initSize)
{
sList = new string[initSize];
return;
}
FirstClass& FirstClass::operator+(const FirstClass& add)
const{
if(add.sList){
int prevSize = this->_size;
this->_size += add._size; // can't do this
string newList[this->_size];
for(int a=0; a<prevSize; a++){
newList[a] = this->sList[a];
}
for(int b=0; b<add._size; b++){
sList[b+prevSize] = add.sList[b];
}
delete [] sList;
this->sList = newList; // or this
}
return *this; // or this
}
EDIT: Thanks for the quick response, and clearing up what I was doing. Here is my revised code.
FirstClass FirstClass::operator+(const FirstClass& add)
const{
FirstClass ma(this->_size + add._size);
if(add.sList){
for(int a=0; a<this->_size; a++){
ma.sList[a] = this->sList[a];
}
for(int b=0; b<add._size; b++){
ma.sList[b+this->_size] = add.sList[b];
}
}
return ma;
}
Upvotes: 0
Views: 228
Reputation: 227468
An addition operator should return a new object, not a reference to one of its operands. It doesn't make any sense for A+B
to modify A
or B
, or return a reference to one of them.
With this in mind, it is easy to see how the operator can be const.
FirstClass operator+(const FirstClass& rhs) const;
You could implement a mutating operator+=
, and implement operator+
in terms of that:
FirstClass& operator+=(const FirstClass& rhs);
FirstClass operator+(const FirstClass& rhs) const
{
FirstClass ret = rhs;
ret += *this;
return ret;
}
or as a non-member:
FirstClass operator+(const FirstClass& lhs, const FirstClass& rhs)
{
FirstClass ret = lhs;
ret += rhs;
return ret;
}
Upvotes: 4
Reputation: 133609
The + operator shouldn't return a reference to an object but rather a new object. Don't confuse +()
with +=()
operator. The latter applies the operation to the lval of the expression while the first returns a new object.
You should return:
FirstClass FirstClass::operator+(const FirstClass& add) const
Assuming this you can understand why the const
requirement has a sense. Without it you would be allowed to modify any of the variables of the implicit this
object which is not allowed. The compilation error is there exactly to tell you: "look you are trying to modify the size but you should return a new object so operands must not be modified".
Upvotes: 1