是否可以创建一个指向函数的“new”运算符/构造函数的函数指针?
如果我想要参数化创建一个对象,我当然可以创建一个函数,在特定类上调用 new 并传递一个指针。我想知道是否可以跳过该步骤并将函数指针传递给 new 运算符本身。
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 function pointer to the new
operator itself.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
boost::lambda 提供 函数包装器 < code>new 和
delete
。 这些可用于轻松地将new
调用转换为函数对象。boost::lambda provides function wrappers for
new
anddelete
. These can be used to easily convert annew
call into a function object.operator new
(以及其他风格)负责分配内存,但不构造对象。事实上它的返回类型是void*
。构造对象的是一个new 表达式,它是语言的一部分,而不是函数。因此不可能形成对它的指针或引用;它就像形成对return
的引用一样毫无意义。operator new
(as well as the other flavours) takes care of allocating memory but does not construct objects. In fact its return type isvoid*
. 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 toreturn
.