Why is void not used in constructor function although it doesn't return any value? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Why is void not used in constructor function although it doesn't return any value?

I am particularly curious to know answer of this question in C++.

2nd Sep 2021, 6:59 PM
Arun Jamson
Arun Jamson - avatar
13 Answers
+ 5
A classes constructor is not a void function and returns a pointer to an instance of the class. The signature of the classes constructor is as so, to signify to the compiler that it is a constructor.
2nd Sep 2021, 7:43 PM
ChaoticDawg
ChaoticDawg - avatar
+ 4
I think the constructor does return a value, although you may not declare it. It is forced to return a pointer to the newly-created object. It would not make sense to return anything else, so it is an error if you try to add your own return.
2nd Sep 2021, 7:45 PM
Brian
Brian - avatar
+ 3
Arun Jamson constructor ia not a member function .
3rd Sep 2021, 3:19 AM
A S Raghuvanshi
A S Raghuvanshi - avatar
+ 2
Arun Jamson, ChaoticDawg I have to apologize for my slightly misleading statements. After some research, I learned that I have been wrong in thinking that constructors return an object reference. They do not!! https://stackoverflow.com/questions/2391233/return-type-of-the-constructor-in-c#2391240 Yes, conceptually, you receive an object reference whenever a constructor is called. But technically, the object reference comes from the new operator. The new operator allocates memory, then calls the constructor (which does not return any value), and new returns the memory reference. Alternatively, if the object is created through type declaration instead of using new during run time, then the compiler just works out the address (it might be relative or absolute) and assigns it at compile time. I also learned that you can be evil if you really want and override the new operator! https://www.geeksforgeeks.org/new-vs-operator-new-in-cpp/ #CodingStretch
4th Sep 2021, 2:15 PM
Brian
Brian - avatar
+ 1
Arun Jamson the reason that the constructor returns the object it creates is because it would be evil for it to do otherwise. Imagine how you would get the object if the constructor would not give it to you.
3rd Sep 2021, 10:10 PM
Brian
Brian - avatar
+ 1
Initialization of the allocated memory is done by the constructor. The new operator merely does the equivalent of a malloc() to obtain the memory, then it is up to the constructor to fill the memory with meaningful data. When the constructor returns, new is done, too.
4th Sep 2021, 10:07 PM
Brian
Brian - avatar
+ 1
When you declare an object as Object a(...); I addressed that in what I typed previously: "if the object is created through type declaration..., then the compiler just works out the address (it might be relative or absolute) and assigns it at compile time."
6th Sep 2021, 4:55 PM
Brian
Brian - avatar
0
Why constructor function returns the object it creates, not Void?
3rd Sep 2021, 3:12 AM
Arun Jamson
Arun Jamson - avatar
0
Can be there should be compilation issues (warning or errors) if you try to return a value from a void function?
3rd Sep 2021, 4:23 AM
Arun Jamson
Arun Jamson - avatar
0
But I have a question which arises then what would some Object be? It can't be a local variable, because returning a (non-primitive) local variable invokes a copy constructor, and how would we write that copy constructor without invoking itself?
4th Sep 2021, 3:59 AM
Arun Jamson
Arun Jamson - avatar
0
Check your question like why would you tell it to not to return when it does not, it is like telling a mute person not to speak
4th Sep 2021, 2:08 PM
Nshuti Alliance
Nshuti Alliance - avatar
0
The lifetime of an object begins when its initialization is complete. I mean, the general procedure is as follows: When an object of type T has to be created, the compiler has to reserve storage for the object , and after that, this storage has to be initialize with a proper value. When initialization is finished, the lifetime of the object begins. How is that initialization performed?
4th Sep 2021, 5:32 PM
Arun Jamson
Arun Jamson - avatar
0
The constructor will never return a null pointer because it returns nothing. In your example Object *a = new Object(...); // (1) the new-operator can return a null pointer or throw a bad_alloc exception. The memory is allocated by the new-operator and on this memory location the constructor is called to initialize the data. But what if I don't use 'new'? What if I write Object a(...); // (2) or even Object a = Object(...); //
6th Sep 2021, 4:10 AM
Arun Jamson
Arun Jamson - avatar