本文介绍了创建一个带有现有基类对象的派生类对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否有可能(或快速解决方法)创建一个定义为派生类的对象,而不在内存中创建基类对象;而派生对象应该引用基类的实际存在的对象(接管其内存驻留)?这是因为速度的原因 - 创建一个派生对象,将数据从基类对象复制到它,然后销毁基本对象需要太多时间。
Is there a possibility of (or fast workaround for) creating an object defined as derived a class without creating base class object in memory; instead the derived object should refer to the actually existing object of base class ("take-over" its memory residence)? This is needed for speed reasons - creating new a derived object, copying data from base class object to it, and then destroying base object takes too much time.
推荐答案
在这种情况下,您可能需要考虑合成而不是继承 - 这将更自然。
You might want to consider composition instead of inheritance in this case - it would be more natural.
这篇关于创建一个带有现有基类对象的派生类对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!