What does slicing mean in C++?

前端 未结 4 1476
暖寄归人
暖寄归人 2020-12-03 18:38

It is mentioned in C++ FAQ site -- \"larger derived class objects get sliced when passed by value as a base class object\", what does slicing mean? Any sample to demonstrate

4条回答
  •  醉话见心
    2020-12-03 19:04

    struct A {
    };
    
    struct B : public A {
      int x;
    };
    
    void f( A a ) {
       // in here you can't access any of B's members - they have ben sliced
    }
    
    int main() {
        B b;
        f( b );   // slice!
    }
    

提交回复
热议问题