Why constructors don't have return types

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Aug 2005
Posts: 598
Reputation: SpS is on a distinguished road 
Solved Threads: 32
SpS's Avatar
SpS SpS is offline Offline
Posting Pro

Why constructors don't have return types

 
0
  #1
Aug 26th, 2005
Can anyone explain me in simple language and better if with an example why constructor don't have return types :!:
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,337
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1459
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Why constructors don't have return types

 
0
  #2
Aug 26th, 2005
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.
  1. // This doesn't make sense!!
  2. int x = CMyClass SomeObject;
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,579
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 709
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Why constructors don't have return types

 
0
  #3
Aug 26th, 2005
>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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 598
Reputation: SpS is on a distinguished road 
Solved Threads: 32
SpS's Avatar
SpS SpS is offline Offline
Posting Pro

Re: Why constructors don't have return types

 
0
  #4
Aug 26th, 2005
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++)
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,337
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1459
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Why constructors don't have return types

 
0
  #5
Aug 26th, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2003
Posts: 46
Reputation: Dante Shamest is an unknown quantity at this point 
Solved Threads: 0
Dante Shamest's Avatar
Dante Shamest Dante Shamest is offline Offline
Light Poster

Re: Why constructors don't have return types

 
0
  #6
Aug 27th, 2005
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,337
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1459
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Why constructors don't have return types

 
0
  #7
Aug 27th, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2003
Posts: 46
Reputation: Dante Shamest is an unknown quantity at this point 
Solved Threads: 0
Dante Shamest's Avatar
Dante Shamest Dante Shamest is offline Offline
Light Poster

Re: Why constructors don't have return types

 
0
  #8
Aug 27th, 2005
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC