![]() |
| ||
| 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 :!: |
| ||
| 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. // This doesn't make sense!! |
| ||
| 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. |
| ||
| 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++) |
| ||
| Re: Why constructors don't have return types Quote:
|
| ||
| Re: Why constructors don't have return types |
| ||
| 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. |
| ||
| 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. |
| All times are GMT -4. The time now is 8:18 pm. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC