By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,973 Members | 1,242 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,973 IT Pros & Developers. It's quick & easy.

Instantiation of an Object only thru "new Operator"

P: n/a
How to restrict an object to be instantiated only using new operator?

I mean, one should not be allowed to create an object like AnObject obj; it
should be AnObject obj = new AnObject;
Jul 19 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a

"Mysooru" <na*******@yahoo.com> wrote in message
news:bg**********@news.mch.sbs.de...
How to restrict an object to be instantiated only using new operator?

I mean, one should not be allowed to create an object like AnObject obj; it should be AnObject obj = new AnObject;


Make the constructor private and write a static member function that uses
new

class AnObject
{
public:
static AnObject* createAnObject(); { return new AnObject(); }
private:
AnObject(); // private ctor
};

AnObject* obj = createAnObject();

john

Jul 19 '05 #2

P: n/a
>> >How to restrict an object to be instantiated only using new operator?
>
>I mean, one should not be allowed to create an object like AnObject obj;it >should be AnObject obj = new AnObject;


And why would you want that?


Perhaps because the object wants to do a "delete this;"?


Isn't it allowed to do it even if it is an automatic object (assuming
a placement new will be made) ?

Jonathan

Jul 19 '05 #3

P: n/a

"Jonathan Mcdougall" <DE******************@yahoo.ca> wrote in message
news:qc********************************@4ax.com...
>How to restrict an object to be instantiated only using new operator?
>
>I mean, one should not be allowed to create an object like AnObject
obj;it
>should be AnObject obj = new AnObject;

And why would you want that?


Perhaps because the object wants to do a "delete this;"?


Isn't it allowed to do it even if it is an automatic object (assuming
a placement new will be made) ?

Jonathan


You can't delete this on an automatic object. What does placement new have
to do with anything?

john
Jul 19 '05 #4

P: n/a
On Mon, 28 Jul 2003 08:28:37 +0100, "John Harrison"
<jo*************@hotmail.com> wrote:

"Jonathan Mcdougall" <DE******************@yahoo.ca> wrote in message
news:qc********************************@4ax.com.. .
>> >How to restrict an object to be instantiated only using new operator?
>> >
>> >I mean, one should not be allowed to create an object like AnObjectobj; >it
>> >should be AnObject obj = new AnObject;
>>
>> And why would you want that?
>
>Perhaps because the object wants to do a "delete this;"?


Isn't it allowed to do it even if it is an automatic object (assuming
a placement new will be made) ?

Jonathan


You can't delete this on an automatic object. What does placement new have
to do with anything?


Nothing. Who said that?
A sad Jonathan

Jul 19 '05 #5

P: n/a
>> >You can't delete this on an automatic object. What does placement new
have
>to do with anything?


Nothing. Who said that?
A sad Jonathan


Quote '(assuming a placement new will be made)'.

Just trying to understand your point, not pick an argument. Perhaps you
could illustrate with some code.


I think irony does not travel very well through cable modems.

I don't know why I said that, I even started an answer like

class A
{
public:
void f()
{
delete this;
this =

and then

const_cast<A*>(this) =

and worst

const_cast<A*>(this) = new (this) A;

before understanding the profound stupidity of my point. I then made
a joke, hoping nobody would notice. Too bad :)

Sorry about that,
Jonathan

Jul 19 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.