السلام عليكم
كنت اتجول في النت فوجدت احد مواضيع الاخ Wajdy Essam حول Operator OverLoading
قرأت الموضوع فلم افهم بعض الامور
هدا الكود
#include <iostream>
using namespace std ;
class Counter
{
private :
int x ;
public :
Counter (int x)
{ this->x = x ; } // here another usage for this
Counter operator++ ()
{
x++;
return *this ;
}
Counter operator++ (int)
{
Counter tmp(*this);
x++;
return tmp;
}
int getX()
{ return x ; }
};
int main ()
{
Counter c(1);
cout << "c = " << c.getX() << endl;
c++;
cout << "c++ : " << c.getX() << endl;
++c;
cout << "++c : " << c.getX() << endl;
Counter d = c ; // here call to copy Constructor , we will explain it later
d = c++;
cout << "d = c++ \n";
cout << "c : " << c.getX() << endl;
cout << "d : " << d.getX() << endl;
d = ++c;
cout << "d = ++c \n";
cout << "c : " << c.getX() << endl;
cout << "d : " << d.getX() << endl;
return 0 ;
}هدا الجزئ لم افهمه جيدا
Counter operator++ ()
{
x++;
return *this ;
}
Counter operator++ (int)
{
Counter tmp(*this);
x++;
return tmp;
}اولا لم افهم فلسفة Operator جيدا 50 % ما فهمته لو امكن من احد توضيح الامر بشكل مبسط
امر اخر لمادا هدا السطر
return *this ;
من دون السطر سيتم زيادة x ونستدعي الدالة getX اليس صحيح ؟
اما هدا السطر فلم افهمه
Counter tmp(*this);
استعمل الـConstructor وامامه tmp و this
ممكن تفسير
فلو تخلينا عن الاسطر
Counter tmp(*this); return tmp;
فستبقى نفس النتيجة
المهم ادا استعمل postfix يكفي اضافة int ( المتغير المرسل ) الى Operator
لقد تهت في هده الامور


