0

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;
}
1
  • 1
    Hint: if I do: int a; int b; int c = a + b; does a or b get modified? Commented Apr 25, 2013 at 16:49

2 Answers 2

4

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; 
}
Sign up to request clarification or add additional context in comments.

3 Comments

Your non-member version modifies lhs, which is operator+ not supposed to do. In addition, using const on a non-member function is not allowed.
@dousin Thanks. It takes the LHS by value, so the first part is not an issue. What is an issue is that it inhibits return value optimization. I will fix that now.
@dousin It was good that you brought my attention to this. I must have been drunk when I posted the original answer!
1

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".

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.