0

I had started learning c++ in which I came to know that if there is a virtual function in the base class then the vptr is created only in the base class & once the object of the class is created at run time then the vptr is set to point to the vtable of the class whose object is created.

But, when I printed the size of the empty class with virtual function then it comes out to be 8 bytes.

#include <bits/stdc++.h>
#include <iostream>
using namespace std;

#pragma pack(1)
class A{
  public:
      virtual void display(){
        cout<<"A Class"<<endl;
      }
};

class B: public A{
  public:
      void display(){
        cout<<"A Class"<<endl;
      }
        void show(){
          cout<<"B Class"<<endl;
        }
};

int main() 
{
    cout<<sizeof(A)<<" "<<sizeof(B);//8 8
    return 0;
}

Above is the code which I ran.

My Question is if vptr is created when the object is created at run time then why here size comes out to be 8 bytes even if the object of the class has not been created yet.

Also one more question that comes out is

#include <bits/stdc++.h>
#include <iostream>
using namespace std;

#pragma pack(1)
class A{
  public:
      virtual void display(){
        cout<<"A Class"<<endl;
      }
};

class B: public A{
  public:

};

int main() 
{
    cout<<sizeof(A)<<" "<<sizeof(B);
    return 0;
}

Here if I leave class B as empty then total how many vtables will be created here ?

I tried using std::is_polymorphic::value for verifying if vtable is created in the table or not but, I am not sure of this function.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
Fedrick
  • 23
  • 5
  • 1
    `sizeof(A)` is a compile-time constant. It can't possibly be different at different points in the program. An object of class `A` doesn't magically grow and shrink in size as the program proceeds. – Igor Tandetnik Dec 03 '22 at 18:45
  • @IgorTandetnik I know sizeof(A) is compile time constant I am saying that if vptr is created after the object is created and object is created at run time then why vptr pointer is included in class without the object at compile time – Fedrick Dec 03 '22 at 18:49
  • In a typical implementation, a class with virtual functions would have space reserved in it for a vtable pointer. When an object of that class is constructed, the constructor would initialize this pointer to point to the vtable for that class; in the same way as it initializes all other non-static data members. This is probably what the article you were reading is trying to tell you. – Igor Tandetnik Dec 03 '22 at 18:49
  • Okay so, you mean the size for the vptr is already reserved in the class that's why even the empty class size with virtual function will be of 8 bytes – Fedrick Dec 03 '22 at 18:52
  • @Fedrick: "*Okay so, you mean the size for the vptr is already reserved in the class that's why even the empty class size with virtual function will be of 8 bytes*" Just as it is for *all* non-static data members of a class. That's the question you're asking when you say `sizeof(X)`; you're asking how many bytes of storage will an object of type `X` consume. – Nicol Bolas Dec 03 '22 at 18:55
  • @Fedrick: Stop spamming all of the C++ version tags. Nothing in your question pertains to *any* of those versions. – Nicol Bolas Dec 03 '22 at 18:56
  • @NicolBolas I am not spamming but, okay will not do that again – Fedrick Dec 03 '22 at 18:58
  • @Fedrick: The term we use on SO for putting a bunch of tags that are unimportant to a question is "tag spamming". – Nicol Bolas Dec 03 '22 at 18:59
  • @NicolBolas Okay apology for the same and thank you for your answer and okay I got the answer for first question but, how many vtables will be there in the program for the second example ? – Fedrick Dec 03 '22 at 19:01
  • I got the answer for the first question but, didn't get for second part of the question can anybody clear my concept for the same – Fedrick Dec 03 '22 at 19:34
  • See [Why should I not #include ?](https://stackoverflow.com/q/31816095) and [Why using namespace std is bad practice](https://stackoverflow.com/questions/1452721). – prapin Dec 03 '22 at 20:55

0 Answers0