473,408 Members | 2,839 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Instantiation of an Object only thru "new Operator"

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
5 3156

"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
>> >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

"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
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
>> >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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Nick Jacobson | last post by:
This question is with regard to the * operator as used for sequence concatenation. There's the well-known gotcha: a = ] b = a*3 b = 4 print b
18
by: Leslaw Bieniasz | last post by:
Cracow, 28.10.2004 Hello, I have a program that intensively allocates and deletes lots of relatively small objects, using "new" operator. The objects themselves are composed of smaller...
24
by: Rv5 | last post by:
Rookie c++ question, but Ive spent the last 5 years doing Java, where everytime I created an object I used new. In c++ I can create my objects without and its confusing me just a little. I have...
1
by: sven_c_t | last post by:
Hi! Probably a newbie question. I´ve been working a bit with the widget toolkit FLTK and I have been wondering why there is such a heavy use of the new operator, when it does not seem to be...
5
by: Sam | last post by:
Hi everyone Could anyone help me understand the usage of the "New" keyword I'm new to VB.Net. 1. Why do we use the "New" keyword on some object type variables such as the myPen of the...
0
by: . | last post by:
http://daviderognoni.blogspot.com?locawapp - MAIN NEWS =========== * add thread * add "Request" object * new "locawapp_main" function * fixed files.py
7
by: Jim Land | last post by:
In the Yahoo Interface Blog, Douglas Crockford wrote about Javascript: "So the rule is simple: The only time we should use the new operator is to invoke a pseudoclassical Constructor function....
14
by: mlw | last post by:
Do not take anything about this, it is not a flame or troll, while I'm not new to Java I favor C++. However, I may need to use it in a contract position, and am concerned that the restrictions it...
3
by: tvnaidu | last post by:
I compiled tinyxml files (.cpp files) and when I am linking to create final exe in Linux, I am getting lot of errors saying "undefiend reference" to "operator new" and "delete", any idea?. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.