| | |
Virtual constructor
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
Why cant we have a virtual constructor?
All the materials give somewhat similar anwer to this question. "First the VPTR Pointer is initialised to it's proper VTABLE by the contructor which is automatically done by the compiler ,,,," or "You don’t ever want to be able to make a call to a virtual function before the VPTR is properly initialized. Of course, the place where initialization can be
guaranteed is in the constructor, ....." or something similar to this.
My doubt is whether the constructor in question base class constructor???
All the materials give somewhat similar anwer to this question. "First the VPTR Pointer is initialised to it's proper VTABLE by the contructor which is automatically done by the compiler ,,,," or "You don’t ever want to be able to make a call to a virtual function before the VPTR is properly initialized. Of course, the place where initialization can be
guaranteed is in the constructor, ....." or something similar to this.
My doubt is whether the constructor in question base class constructor???
Making constructors virtual doesn't make sense.
In inheritance hierarchy objects are constructed from the base down to the lowest derived class. Making constructor virtual means that you would be using a derived class constructor before a base was even created.
What's the purpose of using a derived class if the base hasn't been created yet?
In inheritance hierarchy objects are constructed from the base down to the lowest derived class. Making constructor virtual means that you would be using a derived class constructor before a base was even created.
What's the purpose of using a derived class if the base hasn't been created yet?
The key to eliminating bugs from your code is learning from your mistakes.
•
•
Join Date: Jun 2006
Posts: 147
Reputation:
Solved Threads: 20
You can get the effect of Virtual Constructor by using Factory design pattern...... which doesn't violates the objected oriented rules specifically inheritance structure........ but its helpful. and some times needed....
The vptr not necessarily initialized by the compiler in the constructor it might be initialized during the construction... its totally the implementation dependent thing... i.e. to preserve the polymorphic behavior. Some compiler might not use Vptr and vTable to hold the virtual functions entries..... they might use some other structure to behave polymorphically.......
check out http://www.daniweb.com/techtalkforums/thread18399.html.
get some idea about virutal constructor kind stuff.
Hope you get the basic idea..
The vptr not necessarily initialized by the compiler in the constructor it might be initialized during the construction... its totally the implementation dependent thing... i.e. to preserve the polymorphic behavior. Some compiler might not use Vptr and vTable to hold the virtual functions entries..... they might use some other structure to behave polymorphically.......
check out http://www.daniweb.com/techtalkforums/thread18399.html.
get some idea about virutal constructor kind stuff.
Hope you get the basic idea..
![]() |
Similar Threads
- copy constructor and 2 args constructor help (C)
- c++ (C++)
- How to call constructor for descendant of TObject ? (C++)
- Constructor and Convertion operator are the same,how to avoid memory leak? (C++)
Other Threads in the C Forum
- Previous Thread: simple problem with square root
- Next Thread: Quick search & replace with c-strings?
| Thread Tools | Search this Thread |
#include * adobe ansi append array arrays asterisks binarysearch centimeter changingto char character cm copyimagefile cprogramme creafecopyofanytypeoffileinc csyntax database directory dynamic execv feet fgets file fork framework function getlogicaldrivestrin givemetehcodez global grade gtkwinlinux hacking histogram ide include incrementoperators infiniteloop input interest kernel keyboard kilometer license linked linkedlist linux linuxsegmentationfault list lists locate logical_drives looping lowest match matrix meter microsoft motherboard mqqueue number odf opendocumentformat opensource overwrite owf pattern pdf performance pointer posix probleminc process program programming radix recursion recv research reversing scripting segmentationfault sequential single socket socketprograming standard string systemcall testing threads turboc unix user voidmain() wab whythiscodecausesegmentationfault windows.h windowsapi





