Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why constructors "return" this pointer?

Tags:

c++

I noticed that the constructor will move this to eax before returning. This is a return value or something else?

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  

VS2012 debug mode


I found that new will use its "return value". Seems like 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;
}
like image 841
QingYun Avatar asked Jul 31 '13 03:07

QingYun


People also ask

Does a constructor return a pointer?

A constructor doesn't return anything.

Should you use the this pointer in the constructor?

Some people feel you should not use the this pointer in a constructor because the object is not fully formed yet. However you can use this in the constructor (in the { body } and even in the initialization list) if you are careful.

What do constructors return?

No, constructor does not return any value. While declaring a constructor you will not have anything like return type. In general, Constructor is implicitly called at the time of instantiation. And it is not a method, its sole purpose is to initialize the instance variables.

Why do constructors not return values?

So the reason the constructor doesn't return a value is because it's not called directly by your code, it's called by the memory allocation and object initialization code in the runtime. Its return value (if it actually has one when compiled down to machine code) is opaque to the user - therefore, you can't specify it.


1 Answers

Your second question disagrees with your first. How can new use if ( operator new() == 0 ) return 0; else return constructor(); if constructor() is producing the condition result?

Anyway…

  1. What the compiler does with registers is the compiler's business. Registers tend to hold whatever information is immediately useful, and if the compiler is written with the belief that every time the constructor is used, the object is used immediately afterwards, it may reasonably choose to put this in a register.

    An ABI may require constructors to do this, but I doubt any do. Anyway, such protocols only apply to things exported from libraries, not strictly within programs.

  2. Any new expression does check the result of operator new against 0 before proceeding to initialize an object. operator new may signal failure by returning nullptr (or NULL, etc.).

    This can actually be a problem with placement new expressions, because it represents unavoidable runtime overhead as the given pointer is generally already known to be non-null.

like image 58
Potatoswatter Avatar answered Oct 06 '22 19:10

Potatoswatter