1

I recently watched a YouTube video from "Low Level Learning" where, at 03:38, it was mentioned that "functions with the same signature in the derived class are virtual in the parent by default." I'm unsure if I misunderstood, but it seems to imply that when a derived class has a method with the same signature as one in the parent class, the method in the parent class becomes virtual, even if the virtual keyword is not explicitly used.

I have a few questions that formed in my mind regarding this statement:

  1. Is the statement true? Does having functions with the same signature in the derived class make them automatically virtual in the parent class?

  2. Additionally, is it valid to use sizeof() on a base class to determine if a parent-derived class uses a vtable?

To verify my understanding of this, I created the following program in C++:

#include <iostream>

struct Base {
    int num;

    Base(int num) : num(num) {}

    void greet() {
        std::cout << "Hello " << num << " from Base Class\n";
    }

    float operator()(int n) const { return n * num; }
};

struct Derived : public Base {
    float num;

    Derived(float num) : Base(1), num(num) {}

    void greet() {
        std::cout << "Hello " << num << " from Derived Class\n";
    }

    float operator()(int n) const { return n * num; }
};

int main() {
    Base base(5);
    Derived derived(8.8);

    base.greet();
    derived.greet();

    std::cout << "sizeof(base)    = " << sizeof(base) << "\n";
    std::cout << "sizeof(derived) = " << sizeof(derived) << "\n";

    std::cout << "base(3)    = " << base(3) << "\n";
    std::cout << "derived(3) = " << derived(3) << "\n";
    return 0;
}

Based on my understanding, if a class uses a vtable, it should result in a different expected sizeof() value compared to a class without a vtable. However, in my program, the size remains unchanged, whereas declaring some parent class methods as virtual does increase the size.

I would appreciate clarification on these questions and an explanation. Thank you!

Nkyla
  • 11
  • 3
  • 2
    _functions with the same signature in the derived class are virtual in the parent by default_ This is categorically untrue. The only way to make a function `virtual`, is to declare it as such. – Paul Sanders Jul 29 '23 at 15:38
  • 1
    Don't get hung up on "uses a vtable"; that's an implementation detail. What matters is that marking a function `virtual` makes it a virtual function; if a base class function is not marked `virtual` it isn't virtual, regardless of what a derived class might do. If it is marked `virtual` then it's virtual and in all derived classes that provide a function with the same signature it's virtual, regardless of whether the derived class marks it `virtual`. – Pete Becker Jul 29 '23 at 15:41
  • 1
    This looks like some C guru that tries hard to write really bad C++ code, to "prove" that C code is 20% faster. He just forgets to tell you that his Super fast "well written C" compiles as C++ as well. And runs just as fast. – BoP Jul 29 '23 at 18:44
  • 1
    I think what he meant to say was "Functions in the derived class are implicitly virtual, _if they have the same signature as a virtual function in the parent class_" – chrysante Jul 30 '23 at 09:32

1 Answers1

2

Is the statement true? Does having functions with the same signature in the derived class make them automatically virtual in the parent class?

No, this is false.

Additionally, is it valid to use sizeof() on a base class to determine if a parent-derived class uses a vtable?

sizeof does not reflect, or has anything to do with anything called a "vtable". Not only that, but in the two thousand pages that make up the current C++ standard you will not find a single mention of anything that's described as a "vtable".

If you review my posting history on Stackoverflow you will find me repeatedly saying this: any clown can upload a video to Youtube, or publish random, rambling hallucinations on some blog. Even I can do that. And I did.

If your goal is to learn C++, you will not succeed, very much, by watching random videos or trying to do random coding puzzles. The only time-tested, proven method of learning the most complicated and hardest to learn general purpose programming language in use today is from a good, reputable textbook. It takes money to publish a textbook and no publisher will risk losing a sizable chunk of change on dead trees without vetting, thoroughly, the source material. There is not much of a vetting process for uploaded Youtube videos, or publishing a web site.

Sam Varshavchik
  • 114,536
  • 5
  • 94
  • 148