Question

So I came across something called Virtual Function in C++, which in a nutshell from what I understood is used to enable function overloading in derived/child classes.

So given that we have the following class:

class MyBase{
public:
    virtual void saySomething() { /* some code */ }
};

then when we make a new class that inherits MyBase like this:

class MySubClass : public MyBase{
public:
    void saySomething() { /* different code than in MyBase function */ }
};

the function in MySubClass will execute its own saySomething() function.

To understand it, isn't it same as in Java where you achieve the same by simply writing the same name of the function in the derived class, which will automatically overwrite it / overload it?

Where's in C++ to achieve that you need that extra step, which is declaring the function in base class as virtual?

Thank you in advance! :)

Was it helpful?

Solution 2

Well in c++ a virtual function comes with a cost. To be able to provide polymorphism, overloading etc you need to declare a method as virtual.

As C++ is concerned with the layout of a program the virtual keywords comes with an overhead which may not be desired. Java is compiled into bytecode and execute in a virtual machine. C++ and native assembly code is directly executed on the CPU. This gives you, the developer, a possibility to fully understand and control how the code looks and execute at assembler level (beside optimization, etc).

Declaring anything virtual in a C++ class creates a vtable entry per class on which the entire overloading thing is done.

There is also compile time polymorphism with templates that mitigates the vtable and resolution overhead which has it's own set of issues and possibilities.

OTHER TIPS

Yes you are correct. In Java, all functions are implicitly virtual. In C++ you have a choice: in order to make a function virtual, you need to mark it as such in the base class. (Some folk also repeat the virtual keyword in derived classes, but that is superfluous).

Let's put it this way.

MyBase *ptr; // Pointer to MyBase
ptr = new MySubClass;
ptr->saySomething();

If saySomething is not virtual in MyBase, the base class version will always be called. If it's virtual, then any derived version will be used, if available.

Virtual Function simply a function overloading?

No. "Overloading" means providing multiple functions with the same name but different parameter types, with the appropriate function chosen at compile time. "Overriding" means providing multiple functions within a class heirarchy, with the appropriate function chosen at run time. In C++, only virtual functions can be overridden.

To understand it, isn't it same as in Java where you achieve the same by simply writing the same name of the function in the derived class, which will automatically overwrite it / overload it?

Yes, assuming you mean "override". In Java, methods are overridable by default. This matches Java's (original) philosophy that we should use a 90s-style object-oriented paradigm for everything.

Where's in C++ to achieve that you need that extra step, which is declaring the function in base class as virtual?

Making functions overridable has a run-time cost, so C++ only does that if you specifically request it. This matches C++'s philosophy that you should choose the most appropriate paradigm for your application, and not pay for language facilities you don't need.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top