I know that derived classes can simply "redefine" base class member functions, and that when that function of a derived class object is called, the function defined in the derived class is used, but... Doesn't this render the "virtual" keyword redundant? I have read of some obviously significant differences between these two cases (ie: if you have a base class pointer pointing to a derived class and you call a function, if it is virtual the derived class function will be called, but if not, the base class function will be called).
Put another way, what is the purpose of being able to redefine member functions as non-virtual functions, and is this a commonly used practice?
Personally, it seems to me like it would just get very confusing.
Thanks!
Virtual Functions and Runtime Polymorphism in C++ Runtime polymorphism can be achieved only through a pointer (or reference) of base class type. Also, a base class pointer can point to the objects of base class as well as to the objects of derived class.
If you don't use virtual functions, you don't understand OOP yet. Because the virtual function is intimately bound with the concept of type, and type is at the core of object-oriented programming, there is no analog to the virtual function in a traditional procedural language.
A virtual function is a special type of function that, when called, resolves to the most-derived version of the function that exists between the base and derived class. This capability is known as polymorphism.
A virtual function is a member function which is declared in the base class using the keyword virtual and is re-defined (Overriden) by the derived class. The term Polymorphism means the ability to take many forms. It occurs if there is a hierarchy of classes which are all related to each other by inheritance.
The most common approach on the most common OOP languages (Java, SmallTalk, Python, etc.) is to have, by default, every member function as virtual
.
The drawback is that there is a small performance penalty for every time a virtual call is made. For that reason, C++ lets you choose if you want your methods defined as virtual, or not.
However, there is a very important difference between a virtual and a non-virtual method. For example:
class SomeClass { ... };
class SomeSubclassOfSomeClass : public SomeClass { ... };
class AnotherSubclassOfSomeClass : public SomeClass { ... };
SomeClass* p = ...;
p->someVirtualMethod();
p->someNonVirtualMethod();
The actual code executed when the someVirtualMethod
call is made depends on the concrete type of the referenced pointer p
, depending entirely on SomeClass
subclasses redefinition.
But the code executed on the someNonVirtualMethod
call is clear: always the one on SomeClass
, since the type of the p variable is SomeClass
.
It sounds like you already know the difference between virtual and non-virtual methods, so I won't go into that as others have. The question is, when would a non-virtual method be more useful than a virtual one?
There are cases where you don't want the overhead of having a vtable pointer included in every object, so you take pains to make sure there are no virtual methods in the class. Take for example a class that represents a point and has two members, x
and y
- you might have a very large collection of these points, and a vtable pointer would increase the size of the object by at least 50%.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With