For wrapint = wrapint situation:
wrapint& wrapint::operator=(const wrapint& rhs)
{
// test for equality of objects by equality of address in memory
// other options should be considered depending on specific requirements
if (this == &rhs) return *this;
m_iCurrentNumber = rhs.m_iCurrentNumber;
return *this;
}
I must say the above is a correct signature for an assignment operator. I think the signatures you provided are wrong, or at least I have never seen such a thing in my experience with C++.
If you want to convert from an int to a wrapint and the other way around then you have to provide the following:
1) from int to wrapint - a proper constructor that will allow implicit conversion from int; as a side note you should really make sure that the behavior is intended and within problem scope when working with such implicit conversions (take a look at C++'s explicit keyword for further "enlightenment")
2) from wrapint to int - a proper cast operator
Below is an example:
#include <iostream>
class wrapint
{
public:
wrapint() : m_iCurrentNumber(0)
{
}
// allow implicit conversion from int
// beware of this kind of conversions in most situations
wrapint(int theInt) : m_iCurrentNumber(theInt)
{
}
wrapint(const wrapint& rhs)
{
if (this != &rhs)
this->m_iCurrentNumber = rhs.m_iCurrentNumber;
}
wrapint& operator=(const wrapint& rhs);
operator int ()
{
return m_iCurrentNumber;
}
private:
int m_iCurrentNumber;
};
wrapint& wrapint::operator=(const wrapint& rhs)
{
// test for equality of objects by equality of address in memory
// other options should be considered depending on specific requirements
if (this == &rhs) return *this;
m_iCurrentNumber = rhs.m_iCurrentNumber;
return *this;
}
using namespace std;
int main()
{
// this will be initialized to 0
wrapint theOne;
// this will be 15
wrapint theOtherOne = 15;
cout << "The one: " << theOne << "\n";
cout << "The other one: " << theOtherOne << "\n";
theOne = theOtherOne;
int foobar = theOne;
// all should be 15
cout << "The one: " << theOne << "\n";
cout << "The other one: " << theOtherOne << "\n";
cout << "The foobar: " << foobar << "\n";
return 0;
}