Help with virtual function and virtual inheritance

This is a discussion on Help with virtual function and virtual inheritance within the C++ Programming forums, part of the General Programming Boards category; hi, anyone can help me. i have below codes Code: class A { char k[3]; public: virtual void aa() {} ...

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    5

    Smile Help with virtual function and virtual inheritance

    hi, anyone can help me. i have below codes
    Code:
    class A
    {
    	char k[3];
    public:
    	virtual void aa() {}
    };
    class B : public virtual A
    {
    	char j[3];
    public:
    	virtual void bb() {}
    };
    
    class C : public virtual B
    {
    	char i[3];
    public:
    	virtual void cc() {}
    };
    
    class D : public B, public C
    {
    	char l[3];
    	virtual void dd() {}
    };
    int main (int argc, char *argv[]) {
    	cout << "A: " << sizeof(A) << endl
    		<< "B: " << sizeof(B) << endl
    		<< "C: " << sizeof(C) << endl
    		<< "D: " << sizeof(D) << endl;
    	return 0;
    }
    the output is
    A: 8
    B: 16
    C: 24
    D: 36

    I don't why , especially the last one.
    I know The A is 8, because there is a pointer point to the virtual function table as well.
    In B and C, if there are no virtual, the output would be B: 12, C: 16 and D would be 28.
    so can anybody explain to me why this happens, what's the principle.
    thanks for your help!

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,752
    I had to draw a picture to help myself:

    Name:  hierarchy.gif
Views: 88
Size:  1.9 KB

    Let's consider the culmination of the hierarchy, D. When you make a D you have two ways to make an A: through C's initialization or B's initialization. The use of the virtual keyword makes sure that, whatever actually happens, there is only one A in the class. Which A you get depends on the order of construction of the B and C sub-objects.

    Perhaps the size makes more sense when you consider those facts. Suppose a C is constructed (but before that, A and B are constructed in turn) and then D is constructed. This means that the parts unique to D are 12 bytes long. Four are probably used to contain your array (with one byte for padding). The other 8 are probably the v-table(s). That's my best guess at any rate.

    Maybe if you understand the structure A->B->C->D better, then you should use it?
    Last edited by whiteflags; 08-30-2010 at 11:35 AM.

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by shaxquan View Post
    hi, anyone can help me. i have below codes
    Code:
    class A
    {
        char k[3];
    public:
        virtual void aa() {}
    };
    class B : public virtual A
    {
        char j[3];
    public:
        virtual void bb() {}
    };
    
    class C : public virtual B
    {
        char i[3];
    public:
        virtual void cc() {}
    };
    
    class D : public B, public C
    {
        char l[3];
        virtual void dd() {}
    };
    int main (int argc, char *argv[]) {
        cout << "A: " << sizeof(A) << endl
            << "B: " << sizeof(B) << endl
            << "C: " << sizeof(C) << endl
            << "D: " << sizeof(D) << endl;
        return 0;
    }
    the output is
    A: 8
    B: 16
    C: 24
    D: 36

    I don't why , especially the last one.
    I know The A is 8, because there is a pointer point to the virtual function table as well.
    In B and C, if there are no virtual, the output would be B: 12, C: 16 and D would be 28.
    so can anybody explain to me why this happens, what's the principle.
    thanks for your help!
    Alignment and padding are implementation-defined, so the output can be just about anything. Perhaps you're confused because 'D' has two vtables?
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 10-28-2009, 10:25 AM
  2. Virtual function and multiple inheritance
    By George2 in forum C++ Programming
    Replies: 68
    Last Post: 02-13-2008, 01:15 AM
  3. Performance and footprint of virtual function
    By George2 in forum C++ Programming
    Replies: 8
    Last Post: 01-31-2008, 07:34 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21