开发者

Is it possible to create a function pointer to the a function's `new` operator/constructor?

If I were to wanted to parameterize creating an object, I could of course make a function which called new on a particular class and passed out a pointer. I am wondering if it's possible to skip that step and pass a fu开发者_Go百科nction pointer to the new operator itself.


boost::lambda provides function wrappers for new and delete. These can be used to easily convert an new call into a function object.


operator new (as well as the other flavours) takes care of allocating memory but does not construct objects. In fact its return type is void*. What constructs an object is a new expression, which is part of the language and not a function. So it's not possible to form a pointer or reference to it; it's as meaningless as forming a reference to return.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜