Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   How do I enforce the user create a object via operator new only? (http://www.velocityreviews.com/forums/t460120-how-do-i-enforce-the-user-create-a-object-via-operator-new-only.html)

Xie Yubo 01-28-2007 11:22 PM

How do I enforce the user create a object via operator new only?
 
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!


Ian Collins 01-28-2007 11:27 PM

Re: How do I enforce the user create a object via operator new only?
 
Xie Yubo wrote:
> 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 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.

Jerry Coffin 01-28-2007 11:33 PM

Re: How do I enforce the user create a object via operator new only?
 
In article <1170026540.624521.97930@h3g2000cwc.googlegroups.c om>,
xieyubo@gmail.com says...
> 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

--
Later,
Jerry.

The universe is a figment of its own imagination.

Xie Yubo 01-28-2007 11:45 PM

Re: How do I enforce the user create a object via operator new only?
 


On Jan 29, 7:27 am, Ian Collins <ian-n...@hotmail.com> wrote:
> Xie Yubo wrote:
> > 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 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.


But in this way, the user only use X* p = makeX(); not X* p = new X;


Xie Yubo 01-28-2007 11:47 PM

Re: How do I enforce the user create a object via operator new only?
 
On Jan 29, 7:33 am, Jerry Coffin <jcof...@taeus.com> wrote:
> In article <1170026540.624521.97...@h3g2000cwc.googlegroups.c om>,
> xiey...@gmail.com says...
>
> > 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
>
> --
> Later,
> Jerry.
>
> The universe is a figment of its own imagination.


I have read it. But in that way, the user only use Fred* p =
Fred::create, not Fred* p = new Fred;


Jerry Coffin 01-28-2007 11:51 PM

Re: How do I enforce the user create a object via operator new only?
 
In article <1170028023.228345.169140@k78g2000cwa.googlegroups .com>,
xieyubo@gmail.com says...

[ ... ]

> I have read it. But in that way, the user only use Fred* p =
> Fred::create, not Fred* p = new Fred;


That's right. What you asked for isn't possible, and that's as close as
you can get.

--
Later,
Jerry.

The universe is a figment of its own imagination.

Xie Yubo 01-28-2007 11:56 PM

Re: How do I enforce the user create a object via operator new only?
 
On Jan 29, 7:51 am, Jerry Coffin <jcof...@taeus.com> wrote:
> In article <1170028023.228345.169...@k78g2000cwa.googlegroups .com>,
> xiey...@gmail.com says...
>
> [ ... ]
>
> > I have read it. But in that way, the user only use Fred* p =
> > Fred::create, not Fred* p = new Fred;That's right. What you asked for isn't possible, and that's as close as

> you can get.
>
> --
> Later,
> Jerry.
>
> The universe is a figment of its own imagination.


Is it impossible? Ok, I see. Thanks every one!


Chris 01-28-2007 11:58 PM

Re: How do I enforce the user create a object via operator new only?
 
Xie Yubo wrote:

> On Jan 29, 7:33 am, Jerry Coffin <jcof...@taeus.com> wrote:
>> In article <1170026540.624521.97...@h3g2000cwc.googlegroups.c om>,
>> xiey...@gmail.com says...
>>
>> > 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
>>
>> --
>> Later,
>> Jerry.
>>
>> The universe is a figment of its own imagination.

>
> I have read it. But in that way, the user only use Fred* p =
> Fred::create, not Fred* p = new Fred;



Yes Xie that's right. This will ensure that Fred is dynamically created via
the new operator. So, since Fred::create() calls "new Fred;" then the user
is effectively calling new Fred.

--
Chris

Grizlyk 01-29-2007 09:32 AM

Re: How do I enforce the user create a object via operator new only?
 
Xie Yubo wrote:

> Then the user can only create a C object with:
> C* p = new C;
> and
> C c;
> is forbided.


class A
{
~A(){}

public:
int i;
static void destroy(A*);
};

void A::destroy(A* p){delete p;}

int main()
{

A a; //error
A *p=new A; //ok

delete p; //error
A::destroy(p); //ok
}

Probably you can overload "new" and "delete" operators for your class also.

--
Maksim A Polyanin



Xie Yubo 01-31-2007 05:37 AM

Re: How do I enforce the user create a object via operator new only?
 
On Jan 29, 5:32 pm, "Grizlyk" <grizl...@yandex.ru> wrote:
> Xie Yubo wrote:
> > Then the user can only create a C object with:
> > C* p = new C;
> > and
> > C c;
> > is forbided.

>
> class A
> {
> ~A(){}
>
> public:
> int i;
> static void destroy(A*);
>
> };
>
> void A::destroy(A* p){delete p;}
>
> int main()
> {
>
> A a; //error
> A *p=new A; //ok
>
> delete p; //error
> A::destroy(p); //ok
>
> }
>
> Probably you can overload "new" and "delete" operators for your class also.
>
> --
> Maksim A Polyanin


Wonderful! Thank you very much!



All times are GMT. The time now is 07:56 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.