1

I'm working on my assignment in C++ course.

I have to create operator+= which will add an object to another set of object.

So, how do I implement operator+= here?

class classNew
{
    anotherClass *my_objects;
public:
    // TODO: classNew(int, char const *)
    classNew operator+=(const anotherClass & rhs);
};

int main()
{
    classNew d1(7, "w");
    anotherClass sgs[5];
    // somehow init sgs[0]..[4]?

    for (int i=0; i<sizeof(sgs)/sizeof(*sgs); ++i)
        d1 += sgs[i];
}

UPDATE:

I have something like this

newClass newClass::operator+=(const anotherClass& seg){

this->my_objs[n_seg] = seg;

return *this;

}
8
  • How did they get added without +=? Commented Jul 27, 2012 at 16:31
  • 1
    Put a clean code sample showing what you have so far, it's hard to understand what you want from the writing here. You can keep the question as is, adding the code should clear it up for us :) Commented Jul 27, 2012 at 16:31
  • 1
    I've added a literal code translation of your problem statement - is it correct? Commented Jul 27, 2012 at 16:36
  • 1
    If the code version is accurate, I'll remove the old text: IMO code is better described by code Commented Jul 27, 2012 at 16:39
  • 1
    If there's something omitted, please add it to the code! Commented Jul 27, 2012 at 16:40

1 Answer 1

4

Unless your operator+= is intended to modify the object you're adding, which would be highly unusual, I'd suggest one two simple changes to the signature:

classNew & classNew::operator+=(const anotherClass& rhs);

You always want to return a reference to the class, otherwise you get a copy.

You have a pointer to anotherClass in your class, I assume that this is actually a pointer to an array. You simply have to copy the passed rhs to the appropriate spot in your array, reallocating it and growing it if necessary. If my assumption is incorrect, you just need to do whatever addition is defined as for your class.

If this weren't an assignment I would also suggest replacing the pointer with a std::vector<anotherClass>.

Sign up to request clarification or add additional context in comments.

9 Comments

Hi @Mark, Thank you for the answer here. I've update a bit on my question about what I have written so far for my operator+= but not sure if that is the right way.
@Ali, your code update looks pretty good, but consider what n_seg should be.
@MarkRansom I've almost made the rhs object to be const similar to your suggestion.
I agree, it doesn't look like you have a problem unless n_seg is not <= length(myObjects) - 1 (mind c++ has no length function, but you get the idea :P )
@MarkRansom I'm a bit confuse on this. So say I use to have 10 object delacare then I want to add the one from main to make it 11 object so just use like this->my_objs[n_seg] = seg; this going to work?
|

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.