| | |
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.
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 binary bitmap business c++ c/c++ char class classes code coding commentinghelp compile console conversion count decide delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez graph guess gui homeworkhelp homeworkhelper iamthwee ifpug ifstream incrementoperators infinite input int integer java lib linkedlist linker loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem proficiency program programming project python random read recursion reference rpg string strings temperature template templates test text text-file tree url variable vector video win32 windows winsock word wordfrequency wxwidgets






