本博客主要记录工作中和学习中遇到的C++问题,不定时更新
Object slicing:
引用下stackoverflow上的例子:
class A {
int foo;
};
class B : public A {
int bar;
};
So an object of type B
has two data members, foo
and bar
.
Then if you were to write this:
B b;
A a = b;
Then the information in b
about member bar
is lost in a
.
obejct slicing可以看成是对象没定义的数据的舍弃,也就是舍弃不需要的/不用被编译的部分。 创建衍生类之后如果用基础类表示衍生类的话只有衍生类继承下来的那些会被拷贝。
对象很复杂应该能省去很多时间