8

Possible Duplicate:
Operator overloading

EDIT 2

I was using insert(...) incorrectly, I didn't actually need a '=' operator. Sorry to waste peoples' time. I have voted to close.. 2 votes remain. Please vote.

EDIT

The reason I want an '=' operator is so I can use the insert(...) function on a vector of Derivation objects. At the moment my compiler says:

/usr/include/c++/4.2.1/bits/stl_algobase.h:283: error: no match for 'operator=' in '* __result = * __first'

I have created '==' and '<' operators for my own classes before but I'm struggling to create an '=' operator. My class looks like this (ignore the silly variable names):

class Derivation {
public:
    string                  rc; 
    ImplementationChoice    Y; 
    vector<Derivation>      X;
    vector<string>          D;       
    vector<string>          C;       
    vector<Player>          P, O;   
    vector<Attack>          B;   

    // various functions
    // ...
};

and I want to know what I need to put in

// What do '=' return?  An object of the class right?
Derivation& operator=(const Derivation &d) const {
    // something....
}

Many thanks.

Community
  • 1
  • 1
ale
  • 11,636
  • 27
  • 92
  • 149
  • 2
    At the very least, it should return a reference (`Derivation&`) rather than a new copy of the object. :) – jalf Jul 18 '11 at 14:00
  • Thank you.. editing post now. Sorry.. pretty new to C++ – ale Jul 18 '11 at 14:01
  • 2
    I recommend you read the [operator overloading faq](http://stackoverflow.com/q/4421706/46642). – R. Martinho Fernandes Jul 18 '11 at 14:03
  • If ImplementationChoice already provides an `operator=` it's likely that an appropriate `Derivation::operator=` has already been implicitly declared. (Not putting in an answer since it doesn't really address your questions, just putting this as a heads up.) – Luc Danton Jul 18 '11 at 14:06

7 Answers7

2

This is up to you, really. What do you need the operator to do? Do you want to return a reference, or do you want a copy?

EDIT: Please note that this was rhetorical. What you use this vector for will determine if you need a reference or a copy. For example, if the object your inserting is at any point going to go out of scope before being removed from the vector, you'll want a copy. If not, and you want the original object to be effected when you change the instance in the vector, you'll want a reference. Hope that helps a bit.

MGZero
  • 5,812
  • 5
  • 29
  • 46
  • Please so EDIT in post. I need the '=' operator so I can use insert(...) on a vector of Derivation objects – ale Jul 18 '11 at 14:12
  • I want to be able to do this: `vector d;` `d.insert(d.end(), vec.begin(), vec.end());` – ale Jul 18 '11 at 14:17
  • From you edit and looking at what I want to do, I believe it is a reference that I need. Thank you. – ale Jul 18 '11 at 14:29
2

first remove the const ... then if you really need a copy operator, do something like that and add your own logic (so that it doesn't do just exactly what would be done with the compiler generated copy operator) :

Derivation& operator=(const Derivation& other) {
    this->rc = other.rc; 
    this->Y = other.Y; 
    this->X = other.X;
    this->D = other.D;       
    this->C = other.C;       
    this->P = other.P;
    this->O = other.O;   
    this->B = other.B;
    // ...
    return *this;
}  
rlods
  • 465
  • 2
  • 6
  • 3
    Except that this is ***exactly*** what the compiler-generated copy-assignment operator does... – Armen Tsirunyan Jul 18 '11 at 14:07
  • @Armen - really? I didn't realise that the compiler generated assignment operator calls the assignment for the `vector` members too? – Nim Jul 18 '11 at 14:12
2

First, an assignment operator probably should not be const--

Second, assignment operators usually return a non-const reference to the object that was assigned a value (*this)

antlersoft
  • 14,636
  • 4
  • 35
  • 55
2

You don't need one. The compiler-generated one will do just fine.

Puppy
  • 144,682
  • 38
  • 256
  • 465
  • That depends on how are implemented ImplementationChoice, Player and Attack... – rlods Jul 18 '11 at 14:10
  • really? there are five `vector`s in this `struct`... – Nim Jul 18 '11 at 14:11
  • Please so EDIT in post. I need the '=' operator so I can use insert(...) on a vector of Derivation objects. It doesn't compile without one. It says `/usr/include/c++/4.2.1/bits/stl_algobase.h:283: error: no match for 'operator=' in '* __result = * __first'` – ale Jul 18 '11 at 14:12
1

The standard way to implement an assignment operator is copy-and-swap. This has the advantages of being the most simple way to make an assignment operator that is correct in the face of exceptions and self-assignment. It also defines the assignment operation in terms of the copy-constructor, thus reducing the number of places where your code needs to be changed if you add extra members to the class.

Anyway - here is what it looks like in your case:

class Derivation {
    public:
    string                  rc; 
    ImplementationChoice    Y; 
    vector<Derivation>      X;
    vector<string>          D;       
    vector<string>          C;       
    vector<Player>          P, O;   
    vector<Attack>          B;   

    //You need to add a swap function to your class
    void swap(Derivation& o) {
        rc.swap(o.rc);
        Y.swap(o.Y);//Assuming ImplementationChoice has a swap function (it should!)
        X.swap(o.X);
        D.swap(o.D);
        C.swap(o.C);
        P.swap(o.P);
        O.swap(o.O);
        B.swap(o.B);
    }
    Derivation& operator=(Derivation const& o) {
        Derivation copy(o);
        copy.swap(*this);
        return *this;
    }
    // various functions
    // ...
};
Mankarse
  • 39,818
  • 11
  • 97
  • 141
  • Thank you Mankarse. Did you see my EDIT in my post? Is this what I need if I want to use the vector insert(...) function on a vector of my Derivation objects? Thanks. – ale Jul 18 '11 at 14:16
  • @alemaster - No, the insert function will use the copy-constructor to insert the Derivation objects into the vector. The compiler-generated copy-constructor should be fine for this class. – Mankarse Jul 18 '11 at 14:19
0

to overload assignment operator you should do this

Derivation& operator=(const Derivation &d)  {
    // something....
   return *this
}

This will allow you to do something like.

Deviation a, b, c; //something

c = b = a;

Leon
  • 1,141
  • 13
  • 25
  • I don't think it should be a const method as you're changing the state of the object (assigning it the value of `d`). It's probably legal C++ but doesn't really make sense. – Skizz Jul 18 '11 at 14:03
  • Thank you.. I basically had the single argument parameter (const Derivation &d) from pasting from a '<' operator elsewhere in my code. I can remove that right? Thanks. – ale Jul 18 '11 at 14:07
  • @alemaster remove what. I dont quite understand – Leon Jul 18 '11 at 14:10
  • @Leon - ignore me! I have only just realised the '=' must have exactly one argument. I tried removing `const Derivation &d` but the compiler complained. – ale Jul 18 '11 at 14:14
0

Since - @jalf has not put up an answer, here it is :)

Derivation& operator=(const Derivation &d) {
  // something....
  return *this;
}

You need to return a reference to this instance. this is a keyword which holds a pointer to the instance on which the operator is acting.

Nim
  • 33,299
  • 2
  • 62
  • 101