gpt4 book ai didi

c++从派生类指针获取基类对象?

转载 作者:行者123 更新时间:2023-11-30 03:08:03 27 4
gpt4 key购买 nike

假设我有 Derived* derivedPtr;
我想要 derivedPtr 中的 Base baseObject;

Base baseObject = *derivedPtr;会用适当的 Base 类成员变量创建 baseObject 吗?

谢谢

最佳答案

Object Slicing

Derived* obj = new Derived;
base objOne = (*obj) ; // Object slicing. Coping only the Base class sub-object
// that was constructed by eariler statement.

关于c++从派生类指针获取基类对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5386834/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com