954,483 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Why constructors don't have return types

Can anyone explain me in simple language and better if with an example why constructor don't have return types :!:

SpS
Posting Pro
599 posts since Aug 2005
Reputation Points: 70
Solved Threads: 32
 

Probably because the are not explicitely called in your code, so what good would it do to return a value -- nobody is going to be listening.

The constructor is only called when you instantiate an object. There is no way to catch the return value of the constructor.

// This doesn't make sense!!
int x = CMyClass SomeObject;
Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

>Can anyone explain me in simple language and better if with an
>example why constructor don't have return types
Here's an idea. Show us how it could be done without breaking all kinds of existing code, and you'll answer your own question.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

Thanx alot i guess it actually doesn't make any sense

I was searching net and came across one answer and it said having return types of constructor means taking address of constructor(which is illegal)...can u explain me this....

sorry for asking such queries (i am bit new to C++)

SpS
Posting Pro
599 posts since Aug 2005
Reputation Points: 70
Solved Threads: 32
 
...can u explain me this....

Nope. I don't know what taking the address of a function has to do with returning a value. constructors don't return values because c++ standards and language say they don't. ;)

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 
Dante Shamest
Light Poster
46 posts since Apr 2003
Reputation Points: 13
Solved Threads: 0
 

Another option -- don't put any code in the constructor that might cause it to fail. M$ MFC is good at that -- first instantiate the object and then call its Create() method. The constructor only initializes class data to some default value (normally 0). Then the Create() method initializes all the rest and either returns a value or throws and exception.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

Yes, one could use MFC's 2-stage construction. However, my personal preference is still to to use the constructor to create the object, because I'm used to the RAII paradigm , and my own wrappers for the Windows API use it.

Dante Shamest
Light Poster
46 posts since Apr 2003
Reputation Points: 13
Solved Threads: 0
 

since every function should return a type so do consttructors(they return the type of the class ).but being the special member function according to the previliges provided by the developer we can't access that type
( i think so.) Becoz
class_name cls_var=class_name();
works.
i know that it may create a temporery object and then copy that in memory space in cls_varand then destroy the temporery.
But another aspect may be that it has a internally usable type(constructor type).

mohit_verma
Newbie Poster
1 post since Apr 2010
Reputation Points: 10
Solved Threads: 0
 
Can anyone explain me in simple language and better if with an example why constructor don't have return types :!:


constructors are meant to initialize your data members in your classes. They dont return any type because that was how they were built and programmed to work. Constructors dont even take a void type...thats the beauty of it....the set values to your data members and ensure proper initialization so that your program doesnt have garabge values and such

corby
Junior Poster
118 posts since Feb 2010
Reputation Points: 6
Solved Threads: 5
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You