11

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?

sbi
  • 219,715
  • 46
  • 258
  • 445
Invisible Hulk
  • 119
  • 1
  • 1
  • 3
  • Well, when you call `new Class();`, it returns a pointer to the location of that instance of the class. Is that what you are referring to? – xxpor Nov 23 '10 at 04:08
  • Related : http://stackoverflow.com/questions/3598833/how-does-it-work-test-pobj-new-test-as-constructor-does-not-return-anythin – liaK Nov 23 '10 at 04:47
  • Removed from the `c++-faq` tag. Please [discuss](http://chat.stackoverflow.com/transcript/message/212892#212892) if you disagree. – sbi Dec 21 '10 at 20:10

6 Answers6

35

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.

James McNellis
  • 348,265
  • 75
  • 913
  • 977
  • See Alf P. Steinbach's post here :http://www.velocityreviews.com/forums/t281999-newbie-question-constructor-return-type.html. He said `A constructor does not have a return type, and yes it is _in a sense_ the object itself, which the constructor constructs`. – Invisible Hulk Nov 23 '10 at 04:14
  • I was referring to the last part of his statement. – Invisible Hulk Nov 23 '10 at 04:14
  • 1
    @Invisible: The important part of that statement is "in a sense:" that's not exactly what happens and it's not strictly correct, but it's a good way of thinking about constructors and initialization. – James McNellis Nov 23 '10 at 04:17
  • 2
    Adding to "you can't actually call a constructor" - sometimes, there's a reason to want to construct an object in memory that has already been allocated. The idea of an explicit call to a constructor may be compelling - but in fact even that doesn't work. The "placement new" syntax is used instead. Though in a slight asymmetry, the cleanup action that complements this (destruct the object but don't free the memory) is an explicit destructor call - not a "placement delete". –  Nov 23 '10 at 04:22
  • 1
    An extra thought - within the constructor (as with any method) the object is referenced via the `this` implicit parameter. The pointer to the instance (or rather uninitialised memory) is passed in, not returned out. Though of course the references via `this` are usually as implicit as the parameter itself. –  Nov 23 '10 at 04:28
  • You could also mention that there is no such thing as a pointer to constructor (as opposed to a pointer to member function, which *does* exist). – fredoverflow Nov 27 '10 at 00:42
  • @James, you *could* call a constructor explicitly by saying `empty::empty()`. That's disallowed, but the name `empty::empty` in there refers to the constructor (by translating the injected class name from a reference to a type to a reference to the constructor - that's a special twist in the spec). – Johannes Schaub - litb Dec 10 '10 at 14:48
  • 1
    What about [Terminator().say();](http://coliru.stacked-crooked.com/a/ea20f96174cec700). Looks like reference to created object to me. Sort of... – mip Feb 08 '16 at 23:48
  • 1
    @doc: Yes. It constructs a temporary Terminator object, calls Terminator::say() on that temporary object, then destroys that temporary object. Temporary objects are not destroyed until the end of the full expression in which they were constructed. – James McNellis Feb 08 '16 at 23:53
  • Or unless you use a const reference to prolong a lifetime of temporary object. http://coliru.stacked-crooked.com/a/4a3672ea05d2a10d – mip Feb 08 '16 at 23:57
  • 1
    So, could we say that constructor returns nothing, but it has return by value semantics? :P I imagine if return type was specified a signature would look like `Terminator Terminator::Terminator()` rather than `void Terminator::Terminator()`. – mip Feb 09 '16 at 00:50
11

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.

mbinette
  • 5,094
  • 3
  • 24
  • 32
bill
  • 119
  • 1
  • 2
3

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

Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • 1
    Then, how can this statement be legit?: `MyClass myclass = MyClass();` – hkBattousai Oct 30 '15 at 11:06
  • @hkBattousai There are bunch of special rules handling initializers. Unfortunately it has nothing to do with how to get something looking like being returned from a constructor. Since a constructor even does not have a name (the class name here is handled by special rules of declarator rather than function call's _postfix-expression_), you have no way to refer to its type so the "return type" (if any) does not make sense. – FrankHB Oct 02 '18 at 11:56
2

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.

Siva Prakash
  • 4,626
  • 34
  • 26
0

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.

Craig
  • 162
  • 6
  • All classes/structs have constructors. In this case, there's the "default default constructor" - the do-nothing default constructor that every type gets unless you say otherwise. IIRC, in C++0x it will be a fill-with-zeros implicit default constructor instead. –  Nov 23 '10 at 04:38
  • As other poster, `new` allocates the memory, not the constructor. – edA-qa mort-ora-y Nov 23 '10 at 07:19
-1

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;

uss
  • 1,271
  • 1
  • 13
  • 29
  • Good point, but const references [prolong lifetime of temporaries](http://stackoverflow.com/questions/23685460/do-non-const-references-prolong-the-lives-of-temporaries). Thus ctor behaves like it's returning by value. If it returned const reference you could just `empty & er = const_cast(empty())`. – mip Feb 09 '16 at 01:21