class with virtual functions takes more space - c++

There is such code:
#include <iostream>
class A{
int a;
int fun(){}
};
class B{
int a;
virtual int fun(){}
};
int main()
{
std::cout << sizeof(A) << " " << sizeof(B) << std::endl;
std::cin.get();
return 0;
}
The output is:
4 8
Why class B is 4 bytes bigger than class A?

Any class with a virtual function needs a pointer to the vtable for that class. Therefore, there is a hidden member that's the size of the pointer.
http://en.wikipedia.org/wiki/Virtual_method_table

Related

Size of the classes in case of virtual inheritance(C++)

I saw this question.
this post is so old. so i got different output.
👇 test code
#include <iostream>
class M{
char k[ 3 ];
public:
void m(){};
};
class A{
char k[ 3 ];
public:
virtual void a(){};
};
class B : public A{
char j[ 3 ];
public:
virtual void b(){};
};
class C : public virtual A{
char i[ 3 ];
public:
virtual void c(){};
};
class D : public B, public C{
char h[ 3 ];
public:
virtual void d(){};
};
int main(){
A a;
B b;
C c;
D d;
std::cout << sizeof(M) << std::endl;
std::cout << sizeof(a) << std::endl;
std::cout << sizeof(b) << std::endl;
std::cout << sizeof(c) << std::endl;
std::cout << sizeof(d) << std::endl;
}
👇 and output
3
16
16
32
48
I understand why B have same size as A with this post.
But still i dont get it why C's size is 32.
C has subobject of A(16) + new array(3) + pointer to A(8)
and maybe padding(5).
It makes sense if B didn't reuse the padding, but it doesn't make sense because I thought B was the same size as A because he reused it.
I don't understand that result.
can you help me to get it?!
I search some other post..

Is there any way to replace methods in super class without losing the old definition?

I'm trying to replace a function in super class A in order to extend its functionality in B class, but without losing the old definition. So is there any way to force the A::b method to use a new definition from B class? I have experience in Java, so I know it is possible in this language. Expected output is
A::b
B::a
A::a
Current output is
A::b
A::a
#include <iostream>
using namespace std;
class A {
protected:
static void a() {
cout << "A::a" << endl;
}
public:
void b() {
cout << "A::b" << endl;
a();
}
};
class B: public A {
protected:
static void a() {
cout << "B::a" << endl;
A::a();
}
};
int main()
{
B b;
b.b();
return 0;
}
https://godbolt.org/z/dnY5o8WfK
I think you meant to use virtual keyword instead of static as shown below, since static in this context means that there is no implicit this parameter for the non-static member function a.
#include <iostream>
using namespace std;
class A {
protected:
virtual void a() { //note the virtual keyword
cout << "A::a" << endl;
}
public:
void b() {
cout << "A::b" << endl;
a();
}
};
class B: public A {
protected:
virtual void a() {//note the virtual keyword
cout << "B::a" << endl;
A::a();
}
};
int main()
{
B b;
b.b();
return 0;
}
The output of the above program can be seen here:
A::b
B::a
A::a
By using virtual we're making the member function a to be a virtual member funciton.

C++ multiple inheritance and vtables :with member variable access just not method access

This is modification of question and code presented in this link:
C++ multiple inheritance and vtables.
I understood from this link how vptr & vtable are setup.
My question is how does, bp->OutB() retrieves value of int a, which is member of classA.
I thought we pass this pointer to this method which looks at B part of class C.
Specifically,
Memory layout of classC assuming 32 bit.
&obj.
+0: vptr( pointer to virtual method table of classC( for classA).
+4: value of a.
+8: vptr( pointer to virtual method table of classC( for classB).
+12: value of b.
+16: value of c.
My questions is, we pass &obj+8 as this pointer to when we execute: bp->OutB(), but how does it end up retrieving member: int a of class A in that case.
#include <iostream>
using namespace std;
class A
{
public:
virtual void OutA() = 0;
int a;
};
class B
{
public:
virtual void OutB() = 0;
int b;
};
class C : public A, public B
{
void OutA();
void OutB();
int c;
};
void C::OutA()
{
cout << "Out A " << endl;
}
void C::OutB()
{
cout << "Out B " << " a is: " << a << endl;
}
int main()
{
C obj;
obj.a = 10;
obj.b = 20;
obj.c = 30;
obj.OutA();
obj.OutB();
A* ap = (A*)&obj;
B* bp = (B*)&obj;
ap->OutA();
bp->OutB();
return 0;
}

Something I can not figure out about Vtable and Vptr in Multiple Inheritance

The following code
class B
{
public:
void f1() {}
};
class C
{
public:
void f2() {}
};
class D : public B, public C
{
public:
virtual void f3() {}
};
int main()
{
cout << sizeof(B) << endl;
cout << sizeof(C) << endl;
cout << sizeof(D) << endl;
system("pause");
return 0;
}
gets a result 1 1 8.So why is 8 not 4(in my computer,a pointer takes 4 bytes)?
B,C do not have vptr, and when D has virtual members, D's Vptr is placed in the Vtable of the first inherited class which is B,since the following code
class B
{
public:
virtual void f1() {} //now is virtual
};
class C
{
public:
void f2() {}
};
class D : public B, public C
{
public:
virtual void f3() {}
};
int main()
{
cout << sizeof(B) << endl;
cout << sizeof(C) << endl;
cout << sizeof(D) << endl;
system("pause");
return 0;
}
gets a result 4,1,4. May somebody explain it to me, thanks a lot!

how to use the "sizeof" operator with "virtual" operator [duplicate]

There is such code:
#include <iostream>
class A{
int a;
int fun(){}
};
class B{
int a;
virtual int fun(){}
};
int main()
{
std::cout << sizeof(A) << " " << sizeof(B) << std::endl;
std::cin.get();
return 0;
}
The output is:
4 8
Why class B is 4 bytes bigger than class A?
Any class with a virtual function needs a pointer to the vtable for that class. Therefore, there is a hidden member that's the size of the pointer.
http://en.wikipedia.org/wiki/Virtual_method_table