943,983 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 11079
  • C++ RSS
Aug 26th, 2005
0

Why constructors don't have return types

Expand Post »
Can anyone explain me in simple language and better if with an example why constructor don't have return types :!:
Similar Threads
SpS
Reputation Points: 70
Solved Threads: 32
Posting Pro
SpS is offline Offline
598 posts
since Aug 2005
Aug 26th, 2005
0

Re: Why constructors don't have return types

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.
C++ Syntax (Toggle Plain Text)
  1. // This doesn't make sense!!
  2. int x = CMyClass SomeObject;
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Aug 26th, 2005
0

Re: 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
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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Aug 26th, 2005
0

Re: Why constructors don't have return types

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
Reputation Points: 70
Solved Threads: 32
Posting Pro
SpS is offline Offline
598 posts
since Aug 2005
Aug 26th, 2005
0

Re: Why constructors don't have return types

Quote originally posted by sunnypalsingh ...
...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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Aug 27th, 2005
0

Re: Why constructors don't have return types

Reputation Points: 13
Solved Threads: 0
Light Poster
Dante Shamest is offline Offline
46 posts
since Apr 2003
Aug 27th, 2005
0

Re: Why constructors don't have return types

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Aug 27th, 2005
0

Re: Why constructors don't have return types

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.
Reputation Points: 13
Solved Threads: 0
Light Poster
Dante Shamest is offline Offline
46 posts
since Apr 2003
Apr 12th, 2010
-4

i think so

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).
Last edited by mohit_verma; Apr 12th, 2010 at 10:26 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mohit_verma is offline Offline
1 posts
since Apr 2010
Apr 14th, 2010
0
Re: Why constructors don't have return types
Click to Expand / Collapse  Quote originally posted by SpS ...
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
Reputation Points: 6
Solved Threads: 3
Junior Poster
corby is offline Offline
105 posts
since Feb 2010

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Classes and inheritance
Next Thread in C++ Forum Timeline: Splitting code into files





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC