Equality ( operator== ) Overload
-
I've never overloaded the equality operator and am having difficulty finding example code in MSDN for doing so. I'm guessing it looks a little something like this:
bool CLASS_NAME::operator==( const CLASS_NAME & rhs ) const { //Perform tests on length, size //and any other info needed //return true if all match //false for all other cases }
Is this correct or am I missing something? ________________________________________________________________________ The question "Do computers think?" is the same as "Can submarines swim?" Signature Red Studios Jeryth -
I've never overloaded the equality operator and am having difficulty finding example code in MSDN for doing so. I'm guessing it looks a little something like this:
bool CLASS_NAME::operator==( const CLASS_NAME & rhs ) const { //Perform tests on length, size //and any other info needed //return true if all match //false for all other cases }
Is this correct or am I missing something? ________________________________________________________________________ The question "Do computers think?" is the same as "Can submarines swim?" Signature Red Studios JerythHere is a way to do it
Class YourClass
{
public:
void operator=(const &YourClass c);private:
int iNum;
CString szStr;
}void YourClass::operator=(const &YourClass c)
{
iNum = c.iNum;
szStr = c.szStr;
}Ask not what your application can do for you, Ask what you can do for your application
-
Here is a way to do it
Class YourClass
{
public:
void operator=(const &YourClass c);private:
int iNum;
CString szStr;
}void YourClass::operator=(const &YourClass c)
{
iNum = c.iNum;
szStr = c.szStr;
}Ask not what your application can do for you, Ask what you can do for your application
Excellent example of an assignment operator overload. Unfortunately this was not what the OP wanted, he asked for a equality operator overload! :rolleyes:
-
I've never overloaded the equality operator and am having difficulty finding example code in MSDN for doing so. I'm guessing it looks a little something like this:
bool CLASS_NAME::operator==( const CLASS_NAME & rhs ) const { //Perform tests on length, size //and any other info needed //return true if all match //false for all other cases }
Is this correct or am I missing something? ________________________________________________________________________ The question "Do computers think?" is the same as "Can submarines swim?" Signature Red Studios Jerythbool CLASS_NAME::operator==(CLASS_NAME rhs) const may also work, but I don't think there's anything wrong with your version. There's a nice explanation of operator overloading at http://www.parashift.com/c++-faq-lite/operator-overloading.html#faq-13.5[^] Cheers Steen
-
I've never overloaded the equality operator and am having difficulty finding example code in MSDN for doing so. I'm guessing it looks a little something like this:
bool CLASS_NAME::operator==( const CLASS_NAME & rhs ) const { //Perform tests on length, size //and any other info needed //return true if all match //false for all other cases }
Is this correct or am I missing something? ________________________________________________________________________ The question "Do computers think?" is the same as "Can submarines swim?" Signature Red Studios Jeryth -
Excellent example of an assignment operator overload. Unfortunately this was not what the OP wanted, he asked for a equality operator overload! :rolleyes:
-
I've never overloaded the equality operator and am having difficulty finding example code in MSDN for doing so. I'm guessing it looks a little something like this:
bool CLASS_NAME::operator==( const CLASS_NAME & rhs ) const { //Perform tests on length, size //and any other info needed //return true if all match //false for all other cases }
Is this correct or am I missing something? ________________________________________________________________________ The question "Do computers think?" is the same as "Can submarines swim?" Signature Red Studios JerythWow, got it right on the first guess. Thanks for the confirmation. ________________________________________________________________________ The question "Do computers think?" is the same as "Can submarines swim?" Signature Red Studios Jeryth