| | |
Question about virtual functions
![]() |
•
•
Join Date: Aug 2008
Posts: 91
Reputation:
Solved Threads: 0
My textbook says "Constructors cannot be virtual. (Think about it: constructors are called to create new objects. However, if you don't know what type of object you're trying to create, how do you know which constructor to invoke?)" but I am not sure what it means by this. Is it talking about constructors that dynamically allocate memory?
•
•
•
•
My textbook says "Constructors cannot be virtual. (Think about it: constructors are called to create new objects. However, if you don't know what type of object you're trying to create, how do you know which constructor to invoke?)" but I am not sure what it means by this. Is it talking about constructors that dynamically allocate memory?
Nope. It talking about any constructors.
Think again
: virtual functions was invented for dynamic (in run-time) selection of the true (of derived class) object when you access it via base class pointer or reference.If you call base class constructor (i.e. create object) you have no any other objects except this (now created) base class object. No need (and no sense) to search any other objects via virtual function mechanics.
So a virtual constructor is a senseless artifact. Try to reread the book again...
![]() |
Similar Threads
- File I/O newbie question (C++)
- base and inherited classes with virtual functions (C++)
- A few issues i don't understand about classes and general C++ (yet) (C++)
- Quick question on virtual functions (C++)
- Polymorphism Question (C++)
- C++ Static Linked Libraries Question (C++)
- C++ derived class virtual functions (C++)
- why won't my virtual area function work? (C++)
Other Threads in the C++ Forum
- Previous Thread: Question on const Foo *const
- Next Thread: C++ "Access violation reading location 0x00000021."
Views: 321 | Replies: 3
| Thread Tools | Search this Thread |
Tag cloud for C++
6 algorithm array arrays assignment beginner binary c++ c++borland c/c++ calculator char class classes code compile compiler constructor conversion convert count data delete display dll dynamic encryption error file files filestream forms fstream function functions game givemetehcodez graph graphics gui homework iamthwee image input int integer lazy link linked-list linker list loop loops math matrix member memory newbie number object objects opengl output parameter pointer pointers problem program programming project random read reading recursion recursive reference return server sort spoonfeeding string strings struct student studio template templates text time tree variable vc++ vector video visual win32 window windows winsock wxwidgets






