Invisible Hulk
Invisible Hulk

Reputation: 129

What does a constructor return?

My question is what does a constructor return? This question is not quite different from "What is the return type of a constructor?" I have read somewhere that a constructor returns a complete object implicitly (i.e implicit return type is the name of the class) but it shall not be specified explicitly.

struct empty{};

int main(){
   empty(); //creates a temporary and implicitly a constructor is called
}

So as per my interpretation the implicit return type should be the name of the class, in this case empty. Is my wild interpretation correct?

Upvotes: 12

Views: 26282

Answers (7)

illustager
illustager

Reputation: 55

Reading assembly code is always a good way to tell what happens in the program.

class A {
public:
    A(int x): a(x) {};
    int a;
};

int main() {
    A obj = A(25);

    int b = obj.a;
}

Compiling it here with -O0, I got:

A::A(int) [base object constructor]:
        push    rbp
        mov     rbp, rsp

        mov     QWORD PTR [rbp-8], rdi
        mov     DWORD PTR [rbp-12], esi
        mov     rax, QWORD PTR [rbp-8]
        mov     edx, DWORD PTR [rbp-12]
        mov     DWORD PTR [rax], edx      ; this->a = x

        nop
        pop     rbp
        ret

main:
        push    rbp
        mov     rbp, rsp
        sub     rsp, 16
        lea     rax, [rbp-8]
        mov     esi, 25
        mov     rdi, rax
        call    A::A(int) [complete object constructor]
        mov     eax, DWORD PTR [rbp-8]
        mov     DWORD PTR [rbp-4], eax  ; b = obj.a
        mov     eax, 0
        leave
        ret

When calling a constructor, program will allocate a space for the constructed object at first(involved in sub rsp, 16), which locates at rbp-8 in this example. After that, A::A() is called with two arguments, [rbp-8](aka this pointer) and 25.

A::A() doesn't return anything according to the assembly code, but it just assign 25 to where this points. When used later, obj is replaced with [rbp-8], which was allocated before and was initialized by A::A(), instead of being returned by A::A().

Upvotes: 0

Siva Prakash
Siva Prakash

Reputation: 4997

A constructor doesn't return anything.

Source of confusion:

Book *b = new Book();

Many are confused by the above code, which creates an illusion that the constructor returns a pointer to the newly created object.

When you use new keyword, the compiler allocates required memory and then calls the constructor to create a new object on the allocated memory. Then new returns the pointer to that memory block. The constructor only creates the object and never returns anything.

Upvotes: 3

uss
uss

Reputation: 1309

what about this:

int main() {
const empty &er = empty();
empty *ep = const_cast<empty*>(er); //casting away conentness to make changes in the members
cout<<"\n main ends \n";
//dtor get called here
}

ctor returns a const reference to a memory location(*this), you can cast away the const and use its as a nonconst normal object like empty *e = new e;

Upvotes: -2

bill
bill

Reputation: 105

construct does return something. it returns reference to object that this points to. so the implicit return statement from a constructor looks like

*this;

How is this used?

If you create a class template of something with a "generic" type as member, you call the default zero parameter constructor of the generic type explicitly (i.e., generic() ) in the constructor of your class something and initialize your generic member via the assignment operator and initialization statement of the something constructor. Constructor has to return something or none of that crap I just wrote would work. It's in the book I'm reading...lol.

Upvotes: 8

Alok Save
Alok Save

Reputation: 206536

Constructors do not return anything.
Constructors are called implicitly while object creation to initialize the object being created.

Upvotes: 3

Craig
Craig

Reputation: 162

In C++, if I remember correctly, your code will allocate enough room for an "empty" on the stack, and then call empty's default constructor--as specified by the ()--implicitly passing it a this reference. There is no return. And in your case there is no constructor.

Upvotes: 0

James McNellis
James McNellis

Reputation: 355069

A constructor doesn't return anything. A constructor is called to initialize an object. A constructor can only be used to initialize an object; you can't actually call a constructor explicitly (for one thing, constructors do not have names).

In the example you give, empty() is not a function call expression, it is value initialization. It creates a value-initialized temporary object of type empty.

Upvotes: 35

Related Questions