8
मैं क्यों operator int()
परिभाषित operator+
सी ++ ऑपरेटर के बजाय) ऑपरेटर पूर्णांक (का उपयोग कर +
class D {
public:
int x;
D(){cout<<"default D\n";}
D(int i){ cout<<"int Ctor D\n";x=i;}
D operator+(D& ot){ cout<<"OP+\n"; return D(x+ot.x);}
operator int(){cout<<"operator int\n";return x;}
~D(){cout<<"D Dtor "<<x<<"\n";}
};
void main()
{
cout<<D(1)+D(2)<<"\n";
system("pause");
}
मेरी उत्पादन के बजाय शुरू हो जाती है समझने की कोशिश कर रहा हूँ है:
int Ctor D
int Ctor D
operator int
operator int
3
D Dtor 2
D Dtor 1
आपका * प्रश्न * क्या है? – MikeCAT
@ माइककैट यह पहली पंक्ति में है। अस्पष्ट क्या है? – Rotem
'ऑपरेटर int()' हटाएं और आप देखेंगे कि क्यों, या कम से कम आपको अधिकांश कंपाइलर्स और उनके डिफ़ॉल्ट विकल्पों के साथ चाहिए। – chris