简体   繁体   English

使用sizeof,malloc和cast的C ++对象实例化

[英]c++ object instantiation using sizeof, malloc and cast

In Java I can instantiate an object using the 'Class' method 'newInstance' which for my particular system I find very helpful indeed. 在Java中,我可以使用“类”方法“ newInstance”实例化一个对象,这对我的特定系统确实非常有用。 I am now trying to achieve something similar in C++. 我现在正在尝试在C ++中实现类似的目标。

It has not been obvious to me how this might be achieved but after some reflection .... (do you get it ... huh?) I think that it might be possible by creating a ClassDescription class which has an attribute holding the 'sizeof' the class instance and a method newInstance which mallocs this amount of memory and returns it as a void *. 对我来说,如何实现它还不是很明显,但是经过一番思考....(你明白了吗...是吧?),我认为可以通过创建一个ClassDescription类来实现,该类的属性包含' sizeof'类实例和一个newInstance方法,该方法malloc分配此内存量并将其作为void *返回。 The calling code will then need to cast this appropriately. 然后,调用代码将需要对其进行适当的转换。

Does the C++ language suitably define whether this is valid? C ++语言是否适当地定义了这是否有效?

By the way .. I recognise that I could create a registry holding Factories for the classes which is a backup plan. 顺便说一句..我认识到我可以为类创建一个保存工厂的注册表,这是一个备份计划。 For answers to this question I would value focusing on the specific question of whether what I have discussed will work. 对于这个问题的答案,我将重点放在我所讨论的内容是否有效的特定问题上。

Best Regards 最好的祝福

* Additional Context * The reason for this requirement is to allow a generic library to instantiate classes which the library user is aware of but not the library itself. *附加上下文*此要求的原因是允许通用库实例化库用户知道的类,而不是实例化库本身。 The library will have some meta data to use to achieve this and so could be told the sizeof the class. 该库将具有一些用于实现此目的的元数据,因此可以告知类的大小。 It is 'neater' from the user perspective not to have to add a factory object to the the meta data. 从用户的角度来看,不必将工厂对象添加到元数据是“更整洁”的事情。

This would be valid in some instances. 在某些情况下这将是有效的。 The requirement is that the type must be a "plain old data type" (POD) ( see also this answer ). 要求该类型必须是“普通旧数据类型”(POD)另请参见此答案 )。 If it has anything more complicated (eg virtual member functions, members which have virtual member functions, base classes must also be POD etc.) then it will not work and is undefined behaviour. 如果它有任何更复杂的内容(例如,虚拟成员函数,具有虚拟成员函数的成员,基类也必须是POD等),则它将不起作用,并且行为不确定。

You can check if a type meets these requirements by doing: 您可以通过执行以下操作检查类型是否满足这些要求

#include <type_traits> 

static_assert(std::is_pod<A>::value, "A must be a POD type.");

In general though it probably indicates that you're doing it wrong. 一般而言,这可能表明您做错了。 C++ isn't Java and there's probably a much better way to solve the underlying real problem. C ++不是Java,可能有更好的方法来解决潜在的实际问题。

What you're missing in the malloc and cast method is the construction of the object. 在malloc和cast方法中缺少的是对象的构造。 Using new both allocates memory and constructs it. 使用new既可以分配内存,也可以构造内存。 This includes building v-tables, calling the constructor and so on. 这包括构建v表,调用构造函数等。 Type casting malloc allocated memory is not valid. 类型转换为malloc分配的内存无效。

Note that malloc ing a memory block of the right size gives you just raw memory. 请注意, malloc适当大小的内存块只会为您提供原始内存。 You need to construct an object of the desired class in this memory block. 您需要在此存储块中构造所需类的对象。 This can be achieved using placement new . 这可以通过使用new放置来实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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