c++ 虚基类大小

#include 
using namespace std;
class A {

public:
    A() :ia_(10) {
    }
    virtual void f() {std::cout << "A::f()" << endl;}

private:
    int ia_;
};
class B :virtual public A {
public:
    B() :ib_(20) {}
    void fb() {std::cout << "B::f()" << endl;}
    virtual void f() {std::cout << "B::f()" << endl;}
    virtual void f2() {std::cout << "B::f2()" << endl;}
private:
    int ib_;
};

int main() {
    B b;
    std::cout << "sizeof(b): " << sizeof(b) << endl;
    system("pause");
    return 0;
}

int ia_ + virtual f() + class B:virtual public A+ int ib_ + virtual f() + virtual f2()=
4 + 8+8+4+8+8+8=40

你可能感兴趣的:(c++ 虚基类大小)