Wednesday, 13 July 2016

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


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 X;
vector D;
vector C;
vector P, O;
vector 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.

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...