如何判断对象是否在构造函数上静态或动态分配? [重复]

时间:2021-10-21 19:25:37

Possible Duplicate:
Detect dynamically allocated object?

可能重复:检测动态分配的对象?

I have an object that requires a slightly different construction wether it's instance is staticly or dynamically allocated. The object should only have a single default constructor. So having two constructors, one for each case, and having the user explicitly select the proper constructor is out of the question.

我有一个对象需要稍微不同的构造,它的实例是静态或动态分配的。该对象应该只有一个默认构造函数。因此,有两个构造函数,每个案例一个,并让用户明确选择适当的构造函数是不可能的。

Is there any proper way to acheive this?

有没有适当的方法来实现这一点?

That's how I do this at the moment: I overload the new operator for that object, malloc the memory, and use the returned pointer as a pointer to the (yet un-initialized) instance, and set a specific data member of the object to some magic-value. Then, within the consutrctor, I check the value of the member. If it's the magic-value, then the object is 99.9% dynamically allocated.

这就是我现在这样做的方法:我重载该对象的new运算符,malloc内存,并使用返回的指针作为指向(尚未初始化的)实例的指针,并将对象的特定数据成员设置为一些神奇的价值。然后,在使用者中,我检查成员的值。如果它是魔术值,则该对象动态分配99.9%。

This method haven't yet failed for me under either relase and debug modes, however, it seems like a terrible hack.

在重新调试和调试模式下,这种方法对我来说还没有失败,但是,它似乎是一个可怕的黑客攻击。

3 个解决方案

#1


You should be able to achieve what you want while leaving a single user-accessible default constructor (which will be used for static and auto objects -- you appear to ignore the existence of auto objects, e.g. local variables, so I imagine you want to treat these cases the same).

你应该能够实现你想要的,同时留下一个用户可访问的默认构造函数(它将用于静态和自动对象 - 你似乎忽略了自动对象的存在,例如局部变量,所以我想你想要对待这些案件是一样的)。

Make operator new and a separate constructor both private, and make a public static method (it's a case of the "factory method" design pattern) which only does a return new TheClass(123); (assuming the separate constructor takes for example an integer, but of course you can pick any type of argument you want, as the argument isn't used anyway).

使operator new和一个单独的构造函数都是私有的,并创建一个公共静态方法(这是“工厂方法”设计模式的一种情况),它只返回一个新的TheClass(123); (假设单独的构造函数采用例如一个整数,但当然你可以选择你想要的任何类型的参数,因为无论如何都不使用参数)。

You know you said the object should have a single constructor, but from the user's point of view that's exactly of the class is behaving, and there's no "explicit selection of constructor" on the user's part (he just can't call new explicitly but must go through your supplied factory method, that's all).

你知道你说对象应该有一个构造函数,但是从用户的角度来看,这个类的行为正是如此,而且用户的部分没有“显式选择构造函数”(他只是不能明确地调用new)必须通过你提供的工厂方法,这就是全部)。

#2


Any constructor can be used for static or dynamic allocation. There is no way for you to force the user of the object to use a particular constructor, whichever way he allocates the object.

任何构造函数都可用于静态或动态分配。无论您分配对象的哪种方式,都无法强制对象的用户使用特定的构造函数。

#3


I don't think you can tell in the constructor.

我认为你不能在构造函数中告诉我。

I don't know if this helps, but you can prevent the object from being statically created by making the destructor private. You would also need to have a public "Delete" method that does a delete this for code that dynamically allocates objects of this class -- they would have to do an obj->Delete() rather than delete obj.

我不知道这是否有帮助,但你可以通过使析构函数私有来防止静态创建对象。您还需要一个公共的“删除”方法,对于动态分配此类对象的代码执行删除操作 - 它们必须执行obj-> Delete()而不是删除obj。

#1


You should be able to achieve what you want while leaving a single user-accessible default constructor (which will be used for static and auto objects -- you appear to ignore the existence of auto objects, e.g. local variables, so I imagine you want to treat these cases the same).

你应该能够实现你想要的,同时留下一个用户可访问的默认构造函数(它将用于静态和自动对象 - 你似乎忽略了自动对象的存在,例如局部变量,所以我想你想要对待这些案件是一样的)。

Make operator new and a separate constructor both private, and make a public static method (it's a case of the "factory method" design pattern) which only does a return new TheClass(123); (assuming the separate constructor takes for example an integer, but of course you can pick any type of argument you want, as the argument isn't used anyway).

使operator new和一个单独的构造函数都是私有的,并创建一个公共静态方法(这是“工厂方法”设计模式的一种情况),它只返回一个新的TheClass(123); (假设单独的构造函数采用例如一个整数,但当然你可以选择你想要的任何类型的参数,因为无论如何都不使用参数)。

You know you said the object should have a single constructor, but from the user's point of view that's exactly of the class is behaving, and there's no "explicit selection of constructor" on the user's part (he just can't call new explicitly but must go through your supplied factory method, that's all).

你知道你说对象应该有一个构造函数,但是从用户的角度来看,这个类的行为正是如此,而且用户的部分没有“显式选择构造函数”(他只是不能明确地调用new)必须通过你提供的工厂方法,这就是全部)。

#2


Any constructor can be used for static or dynamic allocation. There is no way for you to force the user of the object to use a particular constructor, whichever way he allocates the object.

任何构造函数都可用于静态或动态分配。无论您分配对象的哪种方式,都无法强制对象的用户使用特定的构造函数。

#3


I don't think you can tell in the constructor.

我认为你不能在构造函数中告诉我。

I don't know if this helps, but you can prevent the object from being statically created by making the destructor private. You would also need to have a public "Delete" method that does a delete this for code that dynamically allocates objects of this class -- they would have to do an obj->Delete() rather than delete obj.

我不知道这是否有帮助,但你可以通过使析构函数私有来防止静态创建对象。您还需要一个公共的“删除”方法,对于动态分配此类对象的代码执行删除操作 - 它们必须执行obj-> Delete()而不是删除obj。