2

What will be the purpose of using non-virtual method in terms of functionality with C++? Is there any merit to change a object's method by its object handle rather than its object type? I found many articles about how virtual method will be utilized but I could not find how non-virtual method will be utilized. Other languages I know of such as Java, Ruby and Python only have virtual method. So functionally non-virtual method is not needed and just be used for performance reason?

OK. I hadn't read the article marked duplicate of. But your answers are still valuable to me in the point the telling the origin of C++ and comparing C++ to other object oriented languages. Thanks everyone to answer.

mongara
  • 33
  • 3
  • 6
    If you don't need polymorphism then why pay for it? C++ makes it (almost painfully) clear that you are going to have extra overhead. – NathanOliver Sep 21 '18 at 13:24
  • 1
    if you dont inherit from a class then there is no use of making a method virtual. Some languages focus primarily on oo-design and make you believe inheritance is the holy grail (i wont say names) so it makes sense that every method is virtual, but thats not the case with c++ – 463035818_is_not_an_ai Sep 21 '18 at 13:25
  • C++ is much older than any of the other languages you listed. There wasn't even such a concept as "virtual functions" at the time C++ came about. There are no virtual functions in C. Virtual functions were a new concept introduced in C++. – Sam Varshavchik Sep 21 '18 at 13:27
  • The complications like that are the reason why well-written c++ programs tend to perform about 4 times better than well written Java program (nothing to talk of ruby or python). – Öö Tiib Sep 21 '18 at 13:27
  • 1
    non-virtual methods open more possibilities for compiler optimizations. – Marek R Sep 21 '18 at 13:29
  • @ÖöTiib I doubt that the fairly minimal overhead from calling virtual functions has much to do with an alleged 4x performance difference. In many cases (especially in those cases in which one wouldn't use polymorphism in C++) a compiler or JIT can determine the run-time type of an object at compile time and cut the call short anyway. – Peter - Reinstate Monica Sep 21 '18 at 13:33
  • @PeterA.Schneider the complication that not all non-static member functions are virtual in C++ is only one from myriad of complications compared to Java. The fact that majority of objects in C++ do not have vtable because these have no virtual functions means that 8 bytes per object is saved. – Öö Tiib Sep 21 '18 at 13:45

5 Answers5

2

The answer is very simple : because C++ is not Java.

Programming languages have different philosophies and different ways to accomplish the same result.

Java (and other "OOP-language-where-every-object-is-GCed-and-is-a-reference-type", like C#) encourage you to think about objects in a very specific way: Inheritance and Polymoprphism are the main ways to achieve flexibility and generalization of code. Objects are almost always reference type, meaning that Car car can actually point to Toyota, Ford and whatever. objects are garbaged-collected and dynamically allocated. All objects anyway inherit from an Object class, so inheritance and dynamic polymorphism is anyway imbued into the language objects by the very language design.

C++ is different. the concept of object might be central to the language, but an object is basically a unit of data and functionality. it's a leaner form of a "real" OOP-language object and it usually allocated on the stack, uses RAII to handle its own resources, and is a value type.

Inheritance and Polymorphism exist, but they are inferior to composition and compile-time-polymorphism (templates).

C++ doesn't encourage you to think about objects as a reference type. objects might be a reference type, they might have virtual functions, but this is only one way to achieve flexibility and generalization in C++, as opposed to Java. you might use templates instead, function pointers and opaque types (a-la C style polymorphism), inheritance + overriding function (a-la Java style), Hence, C++ doesn't force you to take the Java route to flexibility - it gives you the opportunity to choose the best way to accomplish things.

David Haim
  • 25,446
  • 3
  • 44
  • 78
1

When marking a method as virtual, every time such method will be called, the program will have to check the virtual table inside the object that you're calling the method on, it's called dynamic dispatch. It creates quit a bit of overhead compared to normal methods that are resolved using static dispatch.

As big part of C++ is giving the programmer the choice what he wants to do, you can choose if you want static of dynamic linking.

Kaldrr
  • 2,780
  • 8
  • 11
1

C++ method lookup mechanisms won't allow for polymorphism if it's non-virtual. Defining classes as non-virtual will prevent the overhead and confusion.

Have a look at This question and answers

Nina
  • 148
  • 2
  • 16
0

If a method is not virtual, the compiler knows the address in memory where this method's code will be located right at compile time, and can use it right away. If a method is virtual, it will have to be determined at runtime, which implementation should be called, based on the object type. It adds overhead on every call. So, by making a method non-virtual, you make it more efficient.

It should be mentioned that in some languages it's the other way around: the methods are "virtual" by default, but you can explicitly mark them as "non-virtual" (usually called final).

FreeNickname
  • 7,398
  • 2
  • 30
  • 60
0

Non-virtual methods can add additional functionalities specific only to the derived class.

class animal {
public:
    virtual string name() = 0;
};

class rhino :public animal {
public:
    string name() override { return "Rhino"; }
    int getHornSize() { return 10; }  // non-virtual method add functionality only specific to rhino class
};
seccpur
  • 4,996
  • 2
  • 13
  • 21