0

I have the following functions:

Triangle* operator=(Triangle& other) const
{
    for (unsigned int i = 0; i < other.getNumOfPoints(); i++)
    {
        this->addPoint(other.getPoint(i));
    }

    color = other.color;

    //type stays the same
    return this;
}


Point* Polygon::getPoint(int index) const
{
    return _points.at(index);
}

void Polygon::addPoint(Point* p) {
    Point* newp = new Point; //create a copy of the original pt
    newp->setX(p->getX());
    newp->setY(p->getY());
    _points.push_back(newp);
}

I am sure each understand what the objects mean, they are pretty straight forward. First method is located inside Triangle class which inherit from Polygon.

Problem is in the first method when I use

this->addPoint(other.getPoint(i));

Eclipse states its Invalid argument. Can I get an explanation of why is it error when getPoint returns Point pointer and AddPoint function requires a Point pointer?

Thanks in advance.

Nathaniel Johnson
  • 4,731
  • 1
  • 42
  • 69
Ori Refael
  • 2,888
  • 3
  • 37
  • 68
  • 2
    An assignment operator in C++ is usually written to return a reference to the current object, not a pointer. Why the weird implementation? – PaulMcKenzie Nov 04 '15 at 11:23
  • 2
    And it is usually not `const`, because it is supposed to modify the object being assigned to. – juanchopanza Nov 04 '15 at 11:24
  • @PaulMcKenzie can you explain to me why does it matter? because when i assign, either way the object that receives the data is the This..so why should i return Triangle ? what do you suggest to change? – Ori Refael Nov 04 '15 at 11:26
  • @OriRefael so that this is valid: `t1 = t2 = t3` (there `tn` are variables of type `Triangle` – bolov Nov 04 '15 at 11:28
  • @bolov why isn't that valid when working with pointers? – Ori Refael Nov 04 '15 at 11:29
  • 1
    You are not returning a reference when you return a pointer. You are returning a pointer. Second, it is counter-intuitive to have `=` return anything but a reference to the current object. Third, an assignment operator should be similar in design to your copy constructor. Did you implement your copy ctor to return a pointer (which you cannot do anyway)? – PaulMcKenzie Nov 04 '15 at 11:30
  • because that is parsed as `t1 = (t2 = t3)`. The result of `t2 = t3` is a pointer. So `t1 = pointer` isn't valid – bolov Nov 04 '15 at 11:30
  • http://stackoverflow.com/questions/3105798/why-must-the-copy-assignment-operator-return-a-reference-const-reference – bolov Nov 04 '15 at 11:31

1 Answers1

3

The problem is not about the Point* argument of addPoint, but about the implicit this pointer argument:

operator= is marked as a const function, so inside it, this is a pointer-to-const. Thus trying to call a non-const function on it doesn't work.

You need to mark operator= non-const.


On a related note, you're also returning a pointer from operator= and taking the right-hand operand by non-const reference, both of which are weird things to do.

When overloading operators, it's strongly recommended to use the canonical signatures, which you can find e.g. here.

Emil Laine
  • 41,598
  • 9
  • 101
  • 157
  • yeah, i guess coding c++ while you're a newbie after 5 hours makes me blind. thanks. ill accept answer when its possible.. – Ori Refael Nov 04 '15 at 11:23