L'operazione su "questo" puntatore chiama il costruttore?* effettua una chiamata al costruttore?
ho un costruttore definito come segue
Cents(int cents)
{
cout<<"in cents constructor\n";
m_cents = cents;
}
friend Cents operator + (const Cents &c1, const Cents &c2)
{
return Cents(c1.m_cents + c2.m_cents);
}
Cents operator ++ (int)
{
cout<<"In c++ function\n";
Cents c(m_cents);
*this = *this + 1 ;
return c;
}
nella funzione principale che ho STH come ...
Cents c;
cout<<"Before post incrementing\n";
c++; //This part is calling the constructor thrice
Ora, se io sto facendo qualche operazione come *this = *this + 1
. Chiama questo costruttore due volte.
Che cosa sta succedendo esattamente qui. *this
crea un oggetto temporaneo e assegna il valore all'oggetto originale?
Per favore mostraci altro codice. Avete definito il costruttore di copie e l'operatore di assegnazione? –
@ RafałRawicki .... No Non ho alcun costruttore di copia o operatore di assegnazione definito. –
Potresti postare l'esempio completo? Vedi http://sscce.org Penso che il costruttore Cents debba essere chiamato una volta in questa espressione, non due volte. –