| | |
Virtual functions
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2006
Posts: 57
Reputation:
Solved Threads: 2
Of my studying and understanding, Virtual Functions are used only for polymorphism. Use virtual functions ONLY if you know those functions wil be overloaded or overrided in derived classes. Don't make a habit of using virtual in any function because "you may need to overload it later on". Now, for a member function to be virtual in a class, both the base class AND derived class must be able to be instantiated. This is very important otherwise the entire concept of virtual functions is lost.
So summing up virtual functions are important to C++ as they are the major factor in polymorphism.
So summing up virtual functions are important to C++ as they are the major factor in polymorphism.
>>Now, for a member function to be virtual in a class, both the base class AND derived class must be able to be instantiated
Not really. The base class can have pure virtual functions which means it can not be instantiated on its own. The derived class(s) must override pure virtual functions, while it is optional to override the others. A class can have both virtual and pure virtual functions.
>>Don't make a habit of using virtual in any function because "you may need to overload it later on".
Base classes do not always know if the derived classes need to override the virtual functions, so I see no harm in making virtual functions even though the programmer may not know if they are needed or not. I think the main idea here is: does the base class want to allow derived classes to override the function? If yes, then make it virtual.
Not really. The base class can have pure virtual functions which means it can not be instantiated on its own. The derived class(s) must override pure virtual functions, while it is optional to override the others. A class can have both virtual and pure virtual functions.
>>Don't make a habit of using virtual in any function because "you may need to overload it later on".
Base classes do not always know if the derived classes need to override the virtual functions, so I see no harm in making virtual functions even though the programmer may not know if they are needed or not. I think the main idea here is: does the base class want to allow derived classes to override the function? If yes, then make it virtual.
Last edited by Ancient Dragon; Nov 14th, 2006 at 8:30 am.
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.
![]() |
Similar Threads
- Static virtual functions - Reasons (C++)
- Virtual functions, function objects, or what? (C)
- C++ derived class virtual functions (C++)
- How do I work around this? - virtual templates (C++)
- why won't my virtual area function work? (C++)
- Dealing w/ virtual functions (C++)
Other Threads in the C++ Forum
- Previous Thread: Using a struct array
- Next Thread: Font size and layout editing
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll dynamiccharacterarray email encryption error file format forms fstream function functions game givemetehcodez graph gui homeworkhelp iamthwee ifstream input int java lib library lines list loop looping loops map math matrix memory newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search simple sorting spoonfeeding string strings struct temperature template templates text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






