+ 1

What's the most significant advantage of using virtual functions in comparison with pure polymorphism ?

In polymorphism I read it's faster and more efficient to use pointers, so can I reach such a conclusion that the most significant advantage of using virtual functions is using pointers ?

25th Sep 2022, 12:00 PM
Ali_combination
Ali_combination - avatar
11 odpowiedzi
+ 3
from your example, you threat second as a baseclass. From there, if you call the func function, you would get what is defined in the second *if* func was overriden. The virtual keyword marks a function as overridable, it's a concept of polymorphism. There really is no other way to create functions to be overridden. It's a feature that allows you to redefine functionality in a subclass.
26th Sep 2022, 1:52 AM
Apollo-Roboto
Apollo-Roboto - avatar
+ 2
virtual functions and polymorphism won't be a performance issue, you won't ever need to worry about that in any real world case. and yeah, you'll have to override functions all the time, most framework (like Qt) works by giving you a baseclass so you can implement your own features.
26th Sep 2022, 1:11 PM
Apollo-Roboto
Apollo-Roboto - avatar
+ 2
Conceptually, non-virtual inheritance is simpler (and I guess more performant but it really doesn't matter). iirc, if your Base class instances take up 128 bytes of memory, the Derived class' memory layout will look the same, but the extra functions and properties are tacked onto the end. So "upcasting" is as simple as ignoring the extra bytes at the end of the object. Virtual inheritance requires vtables so that's more overhead, but it's been years since I read up about that stuff so I don't feel qualified to talk about those :p
26th Sep 2022, 8:48 PM
Schindlabua
Schindlabua - avatar
+ 1
Virtual functions and pointers are not closely related. A virtual function only means that this function can be overridden by a subclass.
25th Sep 2022, 3:18 PM
Apollo-Roboto
Apollo-Roboto - avatar
+ 1
Apollo-Roboto Arsenic Here I rephrase my question to be clearer: what I learnt in Sololearn C++ courses, is that when declaring a virtual function, we must declare the object a pointer, like this : class baseClass { public: virtual void func () }; class second : public baseClass { ... } ... second cls1; baseClass* mainClass = &cls1; mainClass -> func (); Now we declared the base class a pointer, which is mandatory to use virtual functions. Please let me know, what does using virtual functions make better ? What's the advantage of using it ? Shortening the program ? Making it easier to read ? Or optimization ?
25th Sep 2022, 9:23 PM
Ali_combination
Ali_combination - avatar
+ 1
Maybe you can look at it from a different angle.. in Java for example, every function is virtual. If you have a `Derived* d;` that overrides a `foo`, then it seems weird that `d->foo()` and `((Base*)d)->foo()` would do different things. Like, maybe you have a function that expects.. uh, a TwitterClient, and you would like to pass your selfmade TwitterEmojiClient that also adds eggplant emoji to every tweet, which was derived from the base TwitterClient. But simply because the function expects TwitterClient and you have to upcast your TwitterEmojiClient to make it fit, the TwitterEmojiClient behaves differently? Weird. That's why Java then makes all the public methods virtual and the private ones not virtual. If you have a TwitterEmojiClient it should behave that way even if it has to change shape (in the polymorphism sense). Private functions should usually not be virtual, since otherwise a derived class could override a function that it can't even see, which is even weirder.
26th Sep 2022, 8:47 PM
Schindlabua
Schindlabua - avatar
+ 1
Ayato Sakamaki simply google it or head over to Sololearn C++ course.
27th Sep 2022, 10:48 PM
Ali_combination
Ali_combination - avatar
0
The questions is unclear to me What do you mean by "using pointers" in virtual functions ?
25th Sep 2022, 3:07 PM
Arsenic
Arsenic - avatar
0
Apollo-Roboto ok so, will I ever have to override a particular function in any situation ? ( which can't be done optimizedly without a virtual function )
26th Sep 2022, 8:56 AM
Ali_combination
Ali_combination - avatar
0
Schindlabua I see thank you very much.
27th Sep 2022, 6:59 AM
Ali_combination
Ali_combination - avatar
0
What is virtual function and polymorphism?
27th Sep 2022, 11:52 AM
Ayato Sakamaki
Ayato Sakamaki - avatar