Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ should I use virtual methods?

Let me start by telling that I understand how virtual methods work (polymorphism, late-binding, vtables).

My question is whether or not I should make my method virtual. I will exemplify my dilemma on a specific case, but any general guidelines will be welcomed too.

The context:

I am creating a library. In this library I have a class CallStack that captures a call stack and then offers vector-like access to the captured stack frames. The capture is done by a protected method CaptureStack. This method could be redefined in a derived class, if the users of the library wish to implement another way to capture the stack. Just to be clear, the discussion to make the method virtual applies only to some methods that I know can be redefined in a derived class (in this case CaptureStack and the destructor), not to all the class methods.

Throughout my library I use CallStack objects, but never exposed as pointers or reference parameters, thus making virtual not needed considering only the use of my library.

And I cannot think of a case when someone would want to use CallStack as pointer or reference to implement polymorphism. If someone wants to derive CallStack and redefine CaptureStack I think just using the derived class object will suffice.

Now just because I cannot think polymorphism will be needed, should I not use virtual methods, or should I use virtual regardless just because a method can be redefined.


Example how CallStack can be used outside my library:

if (error) {
  CallStack call_stack; // the constructor calls CaptureStack
  for (const auto &stack_frame : call_stack) {
    cout << stack_frame << endl;
  }
}

A derived class, that redefines CaptureStack could be use in the same manner, not needing polymorphism:

if (error) {
  // since this is not a CallStack pointer / reference, virtual would not be needed.
  DerivedCallStack d_call_stack; 
  for (const auto &stack_frame : d_call_stack) {
    cout << stack_frame << endl;
  }
}
like image 863
bolov Avatar asked Jul 18 '26 09:07

bolov


1 Answers

If your library saves the call stack during the constructor then you cannot use virtual methods.

This is C++. One thing people often get wrong when coming to C++ from another language is using virtual methods in constructors. This never works as planned.

C++ sets the virtual function table during each constructor call. That means that functions are never virtual when called from the constructor. The virtual method always points to the current class being constructed.

So even if you did use a virtual method to capture the stack the constructor code would always call the base class method.

To make it work you'd need to take the call out of the constructor and use something like:

CallStack *stack = new DerivedStack;
stack.CaptureStack();

None of your code examples show a good reason to make CaptureStack virtual.

like image 95
Zan Lynx Avatar answered Jul 21 '26 02:07

Zan Lynx



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!