如何强制用户仅通过operator new创建对象? [英] How do I enforce the user create a object via operator new only?
问题描述
大家好,
我有一个问题,你能帮助我吗?
如何强制用户通过new new创建一个对象?
例如,有一个C类:
class C
{
.. ...
}
然后用户只能创建一个C对象:
C * p = new C;
和
C c;
是禁止的。
有没有办法实现它?
谢谢!
Hello every,
I have a question, could you help me?
How do I enforce the user create a object via operator new only?
For example, there is a class C:
class C
{
.....
}
Then the user can only create a C object with:
C* p = new C;
and
C c;
is forbided.
Are there some ways to implement it?
Thanks!
推荐答案
Xie Yubo写道:
Xie Yubo wrote:
你好每一个,
我有一个问题,你能帮助我吗?
如何强制用户通过运营商创建一个对象只有新的?
例如,有一个C类:
class C
{
.....
}
然后用户只能创建一个C对象:
C * p =新C;
和
C. c;
是禁止的。
有没有办法实现它?
Hello every,
I have a question, could you help me?
How do I enforce the user create a object via operator new only?
For example, there is a class C:
class C
{
.....
}
Then the user can only create a C object with:
C* p = new C;
and
C c;
is forbided.
Are there some ways to implement it?
Make构造函数私有和私有一个朋友函数返回一个
类的实例。
class X
{
X(){}
朋友X * makeX(){返回新X; }
};
-
Ian Collins。
Make the constructor private and private a friend function to return an
instance of the class.
class X
{
X() {}
friend X* makeX() { return new X; }
};
--
Ian Collins.
在文章< 11 ********************* @ h3g2000cwc.googlegroups.c om>,
xi ***** @ gmail.com 说...
In article <11*********************@h3g2000cwc.googlegroups.c om>,
xi*****@gmail.com says...
Hello every,
我有一个问题,你能帮帮我吗?
如何强制用户只通过operator new创建一个对象?
Hello every,
I have a question, could you help me?
How do I enforce the user create a object via operator new only?
[...]
FAQ,16.21
-
后来,
杰瑞。
宇宙是自己想象的虚构。
[ ... ]
FAQ, 16.21
--
Later,
Jerry.
The universe is a figment of its own imagination.
1月29日上午7:27,Ian Collins< ian-n ... @ hotmail.comwrote:
On Jan 29, 7:27 am, Ian Collins <ian-n...@hotmail.comwrote:
谢Yubo写道:
Xie Yubo wrote:
你好每一个,
我有一个问题,你能帮助我吗?
如何强制用户仅通过operator new创建对象?
Hello every,
I have a question, could you help me?
How do I enforce the user create a object via operator new only?
例如,有一个C类:
For example, there is a class C:
class C $ / $
{
.....
}
class C
{
.....
}
然后用户只能创建一个C对象:
C * p = new C;
和
C c;
是禁止的。
Then the user can only create a C object with:
C* p = new C;
and
C c;
is forbided.
有没有办法实现它?让构造函数私有和私有的朋友函数返回
Are there some ways to implement it?Make the constructor private and private a friend function to return an
班级实例。
class X
{
X(){}
朋友X * makeX(){返回新X; }
}; -
Ian Collins。
instance of the class.
class X
{
X() {}
friend X* makeX() { return new X; }
};--
Ian Collins.
但是这样,用户只能使用X * p = makeX();不是X * p =新X;
But in this way, the user only use X* p = makeX(); not X* p = new X;
这篇关于如何强制用户仅通过operator new创建对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!