简体   繁体   中英

what happens when assign a derived class object to a base class object(I mean object assginment, not pointer)

I know that when assign the derived object to base object, the slicing happens. The first part of derived class object will copied to the base object. And here is my quesition:

  1. Will the vptr(virtual table pointer) be copied to base class object?
  2. If vptr is not copied to base class object(I guess, for the object assignment will not cause polymorphism), why? Is there any rule to support that?

No, no vtable pointer is copied. Nor could it be, since the destination object is truly of base class type both before and after the assignment.

Virtual methods called on a sliced object will invoke the base class implementations.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM