开发者

Creating an assignment (=) operator for class in C++ [duplicate]

开发者 https://www.devze.com 2023-03-21 05:49 出处:网络
This question already has answers here: Closed 11 years ago. Possible Duplicate: Operator overloading EDIT 2
This question already has answers here: Closed 11 years ago.

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                  r开发者_运维知识库c; 
    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.


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)


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


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;
}  


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.


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
    // ...
};


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;


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消