使用现有基类对象创建派生类对象?
是否有可能(或快速解决方法)创建定义为派生类的对象,而无需在内存中创建基类对象;相反,派生对象应该引用基类实际存在的对象(“接管”其内存驻留)?出于速度原因,这是需要的 - 创建新的派生对象,将数据从基类对象复制到它,然后销毁基对象需要太多时间。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
在这种情况下,您可能需要考虑组合而不是继承 - 这会更自然。
You might want to consider composition instead of inheritance in this case - it would be more natural.
我不会使用该语言支持的
class
构造。如果您需要一些小而灵活的东西,请考虑编写一个struct
并使用函数指针实现您自己的v-table
。与 Linux 内核中的情况大致相同。请注意,面向对象编程几乎可以用任何语言完成,不一定是支持它的语言。然后,您可以动态切换虚拟表指针,并可能执行一些
realloc
,以添加派生类型所需的字段。最后,您可以将所有这些打包在一个常规的类中,该类没有任何动态方法,只是将所有调用委托给所描述的内部结构。这不应该施加任何内存或计算开销。
编辑:实际上我想 realloc 不是正确的方法。这是一个涉及底层操作系统并需要上下文切换的例程。只要您已经分配了适当的内存块,几乎总是调用
copy
会更快。如果您对速度感兴趣,那么也许还可以考虑实现您自己的内存管理或使用 boost 等库提供的替代实现之一。I wouldn't use the
class
construct that is supported by the language. If you need something small and that flexible consider writing astruct
and implementing your ownv-table
's using function pointers. Much the same way as this is done for example in the Linux kernel. Note that object oriented programming can be done in almost any language, not necessarily one that supports it.You could then switch the v-table pointer on the fly and possibly perform some
realloc
in order to add the fields that are required by the derived type.In the end you could package all of this in a regular
class
that doesn't have any dynamic methods and just delegates all the calls to the described internal structure. This shouldn't impose any memory or computational overhead.EDIT: Actually I guess
realloc
is not the way to go. This is a routine that engages the underlying operating system and requires a context switch. Almost always callingcopy
will be faster provided you have the appropriate memory block already allocated. If you're interested in speed, then maybe consider also implementing your own memory management or using one of the alternative implementation provided by libraries such as boost.我不认为你能做你想做的事。
考虑:
d1 和 d2 要么是不同的对象,包括不同的 b 基底,要么它们是同一个对象。
现在,您可以通过使您的 b-substrate 成为 d 类的静态成员来伪造它。
I don't think you can do what you seem to want to do.
Consider:
Either d1 and d2 are distinct objects, including distinct b-substrates, or they are the same object.
Now, you MIGHT be able to FAKE it by making your b-substrate a static member of your d class.
如果要动态调用派生类,v表是必不可少的。
因此,也许您可以实现没有数据成员的“基类”,并传入虚拟函数在调用期间作为参数所需的数据或数据指针。
它会节省内存,但会花费更多时间。
If you want dynamic call derived class, the v-table is indispensable.
So maybe you can implement the "base class" with no data member, and pass in data or pointer of data that your virtual functions need as argument during calling.
It'll save memory but cost more time.
如果您希望创建基类的一个版本并让所有对象继承或派生自同一个实例,那么您可以在基类中将事物声明为静态。静态意味着类的每个实例都有一个版本。
即
以这种方式,您不声明基类项,而是通过静态变量继承基类的实例,这基本上意味着所有
FooDerived
都在查看相同的内存块FooBase::IDCnt
和FooBase::ObjCnt
。If you wish to create one version of a base class and have all objects inherit or be derived from the same instance, then you can declare things as static in the base class. Static means one version of it for every instance of a class.
i.e.
In this manner you don't declare a base class item, instead, the instance of base class is inherited through the static variables which basically means all
FooDerived
are looking at the same block of memory forFooBase::IDCnt
andFooBase::ObjCnt
.您考虑过工厂设计模式吗?
你的基类只需要知道你想创建什么样的派生类
Did you consider Factory design pattern ?
Your base class only needs to know what kinda derived class you wanna create