+ Start a Discussion
deepak kumawatdeepak kumawat 


why is not return type of the constructor in class ???
Chandra Sekhar CH N VChandra Sekhar CH N V
Constructor will be automatically invoked when an object is created and since the value is its object itself, there is not need for a return type.
Hi Deepak,

You don't specify the return type of the constructor, but internally it returns the objects of that type.
deepak kumawatdeepak kumawat
Yes , it's ok , But I want details in brief , can any one tell me
Amit Chaudhary 8Amit Chaudhary 8
Please check below post. I hope that will help you

A constructor is code that is invoked when an object is created from the class blueprint. You do not need to write a constructor for every class. If a class does not have a user-defined constructor, an implicit, no-argument, public one is used.

The syntax for a constructor is similar to a method, but it differs from a method definition in that it never has an explicit return type and it is not inherited by the object created from it.

NOTE:- What actually happens with the constructor is that the runtime uses type data generated by the compiler to determine how much space is needed to store an object instance in memory, be it on the stack or on the heap.
This space includes all members variables and the vtbl. After this space is allocated, the constructor is called as an internal part of the instantiation and initialization process to initialize the contents of the fields.
Then, when the constructor exits, the runtime returns the newly-created instance. 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.

Please let us know if this will help you