| | |
Why constructor can not be defined as virtual
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
google is a wonderful invention 
http://www.codersource.net/published...ctions_in.aspx

•
•
•
•
A constructor cannot be virtual because at the time when the constructor is invoked the virtual table would not be available in the memory. Hence we cannot have a virtual constructor.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
I think the only way to create a "virtual constructor" is to make a virtual method that returns the object inquestion, where derived classes return a copy of their type instead of the base classes return implementation.
By casting a derived type to a base type and invoking the virtual constructor method, you're constructing an object virtually, therefore it's safe to say it is a "virtual constructor".
By casting a derived type to a base type and invoking the virtual constructor method, you're constructing an object virtually, therefore it's safe to say it is a "virtual constructor".
![]() |
Similar Threads
- C++ Performance Tips (C++)
- Modified Copy Constructor Difficulty (C++)
- default constructor&class (C++)
- Class Vectorization requirements (C++)
- C++ tips (C++)
- unresolved external error upgrading 6.0-7.1 (C++)
- accessing private data members (C++)
Other Threads in the C++ Forum
- Previous Thread: problem reading a in reverse order
- Next Thread: writing to a file
Views: 1223 | Replies: 3
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api array arrays based beginner binary bmp c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete deploy dll download dynamic dynamiccharacterarray encryption error file format forms fstream function functions game givemetehcodez graph gui homeworkhelp iamthwee ifstream input int java lib library lines linker list loop looping loops map math matrix memory newbie news number output pointer problem program programming project python random read recursion recursive reference return rpg search simple sort spoonfeeding string strings struct temperature template templates text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






