Constructor RRS feed

  • Question

  • Is constructor return any value internally ?

    This question answer actually i know constructor never return any value but one of my friend argue that constructor returning value internally if it correct plz explain me .

    If yes then explain me what value and what time it returning?

    S.K Nayak

    Monday, January 5, 2015 8:12 AM


All replies

  • Hi,

    It has been ask before.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Proposed as answer by RJP1973 Monday, January 5, 2015 11:05 AM
    • Marked as answer by SURYA KANT NAYAK Monday, January 12, 2015 10:32 AM
    Monday, January 5, 2015 9:27 AM
  • A constructor implicitly returns a reference to the constructed object, i.e. an instance of the class, or the object itself depending on whether the type is a reference type or value type.

    Please remember to mark helpful posts as answer and/or helpful.

    Monday, January 5, 2015 9:45 AM
  • No,


    A class can have any number of constructors and constructors don’t have any return type, not even void.


    Monday, January 5, 2015 10:36 AM
  • The answers above may appear to be contradictory, but this is just because constructors are a bit of a special case!

    In an attempt to clarify:

    Nirosha is correct in that you never actually specify a return type in a constructor yourself. Constructors are always declared with no return type, unlike other methods (see C# classes for example of a class definition with a couple of constructors and one standard method).

    However, as Magnus says, constructors implicitly (i.e. "secretly") always return the object that has just been created, which is why you should never (and can't) actually specify a return type yourself.

    Hope that helps.

    Monday, January 5, 2015 11:10 AM
  • 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. 

    • Proposed as answer by Ammar.Zaied Monday, January 5, 2015 1:18 PM
    Monday, January 5, 2015 1:18 PM