0

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! :)

4 Answers 4

6

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).

Sign up to request clarification or add additional context in comments.

Comments

5

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.

2 Comments

So basically virtual functions are creating overhead. But if that is so, and Java has virtual methods by default, then isn't java automatically slower than C++? (just an observation) Given the fact that in C++ you can choose which functions are virtual?
@gEdringer this is correct in some cases. The byte code indirection and execution in a virtual machine is typically slower then directly executed assembler code. However slow needs further definition. If you compare for loops you might see a difference, in other scenarios you may not, as pure CPU power is not always the bottleneck. Java on the other hand may be "faster" when developing an application, which could yield a higher ROI than some extra CPU cycles. Every language has it's purpose. This topic itself does not fit into a comment space :)
1

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.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.