The purpose of it is the operations within the overloaded method, but what would be it’s true purpose?
class CModeType
{
public:
CModeType(){m_nModeType=1;}
~CModeType(){}
int m_nModeType;
CModeType& operator&&( const CModeType& rModeType );
};
CModeType& CModeType::operator &&( const CModeType& rModeType )
{
this->m_nModeType += rModeType.m_nModeType;
return *this;
}
int _tmain(int argc, _TCHAR* argv[])
{
CModeType Mode;
CModeType Mod2;
Mode && Mode;
cout << Mode.m_nModeType << endl; //output:2
return 0;
}
I would also ask about || but I believe the answer will help me to understand it so.
For built-in types, the
&&operator is “logical-and”, so thatx && yis true if and only if bothxandyare true when evaluated in a boolean context.For built-in types, the
&&operator is short-circuited, so that the second argument is never evaluated if the first one is already false, so you can say things likeif (p && *p == 10)without risking a null-pointer dereferencing. For the overloadedoperator&&of a user-defined type, this is not true, and it is just an ordinary function (so arguments are evaluated in an indeterminate order). Beware!