| | |
Why constructors don't have return types
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
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.
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)
// This doesn't make sense!! int x = CMyClass SomeObject;
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.
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.
![]() |
Similar Threads
- Covariant return types (Java)
- Taking address of constructors?? (C++)
Other Threads in the C++ Forum
- Previous Thread: Taking address of constructors??
- Next Thread: Can we access Databases through C or C++
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math memory multiple news node number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






