Ho notato che il costruttore si sposterà da this
a eax
prima di tornare. Questo è un valore di ritorno o qualcos'altro?perché i costruttori "restituiscono" questo puntatore?
class CTest {
int val_;
public:
CTest() {
0093F700 push ebp
0093F701 mov ebp,esp
0093F703 sub esp,0CCh
0093F709 push ebx
0093F70A push esi
0093F70B push edi
0093F70C push ecx
0093F70D lea edi,[ebp-0CCh]
0093F713 mov ecx,33h
0093F718 mov eax,0CCCCCCCCh
0093F71D rep stos dword ptr es:[edi]
0093F71F pop ecx
0093F720 mov dword ptr [this],ecx
val_ = 1;
0093F723 mov eax,dword ptr [this]
0093F726 mov dword ptr [eax],1
}
0093F72C mov eax,dword ptr [this]
0093F72F pop edi
0093F730 pop esi
0093F731 pop ebx
0093F732 mov esp,ebp
0093F734 pop ebp
0093F735 ret
modalità debug VS2012
ho scoperto che new
userà il suo "valore di ritorno". Sembra if(operator new() == 0) return 0; else return constructor();
class CTest {
int val_;
public:
CTest() {
val_ = 1;
__asm {
mov eax, 0x12345678
pop edi
pop esi
pop ebx
mov esp,ebp
pop ebp
ret
}
}
};
int main() {
CTest *test = new CTest; // test == 0x12345678
return 0;
}
I costruttori non restituiscono nulla, offrono solo l'opportunità di inizializzare l'oggetto creato. –
È un'ipotesi selvaggia, ma suppongo che sia per supportare la costruzione di sottoclassi. – Mehrdad
È un dettaglio di implementazione. Il costruttore standard Per C++ non restituisce nulla e non dovresti fare affidamento su questo. [alcune discussioni SO correlate] (http://stackoverflow.com/questions/10356109/c-constructors-have-no-return-type-just-exactly-why) – Tony