What's the difference between a derived object and a base object in c++?

前端 未结 10 1820
一个人的身影
一个人的身影 2021-01-01 06:23

What\'s the difference between a derived object and a base object in c++,

especially, when there is a virtual function in the class.

Does the derived object

相关标签:
10条回答
  • 2021-01-01 07:07

    Derived is Base, but Base is not a Derived

    0 讨论(0)
  • 2021-01-01 07:11

    A base object is one from which others are derived. Typically it'll have some virtual methods (or even pure virtual) that subclasses can override to specialize.

    A subclass of a base object is known as a derived object.

    0 讨论(0)
  • 2021-01-01 07:12

    let's have:

    class Base {
       virtual void f();
    };
    
    class Derived : public Base {
       void f();
    }
    

    without f being virtual (as implemented in pseudo "c"):

    struct {
       BaseAttributes;
    } Base;
    
    struct {
       BaseAttributes;
       DerivedAttributes;
    } Derived;
    

    with virtual functions:

    struct {
       vfptr = Base_vfptr,
       BaseAttributes;
    } Base;
    
    struct {
       vfptr = Derived_vfptr,
       BaseAttributes;
       DerivedAttributes;
    } Derived;
    
    struct {
       &Base::f
    } Base_vfptr
    
    struct {
       &Derived::f
    } Base_vfptr
    

    For multiple inheritance, things get more complicated :o)

    0 讨论(0)
  • 2021-01-01 07:13

    a public colon. ( I told you C++ was nasty )

    class base { }
    class derived : public base { }
    
    0 讨论(0)
提交回复
热议问题