473,667 Members | 2,650 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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!

Jan 28 '07 #1
12 1280
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.
Jan 28 '07 #2
In article <11************ *********@h3g20 00cwc.googlegro ups.com>,
xi*****@gmail.c om 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.
Jan 28 '07 #3


On Jan 29, 7:27 am, Ian Collins <ian-n...@hotmail.co mwrote:
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;

Jan 28 '07 #4
On Jan 29, 7:33 am, Jerry Coffin <jcof...@taeus. comwrote:
In article <1170026540.624 521.97...@h3g20 00cwc.googlegro ups.com>,
xiey...@gmail.c om 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;

Jan 28 '07 #5
In article <11************ **********@k78g 2000cwa.googleg roups.com>,
xi*****@gmail.c om 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.
Jan 29 '07 #6
On Jan 29, 7:51 am, Jerry Coffin <jcof...@taeus. comwrote:
In article <1170028023.228 345.169...@k78g 2000cwa.googleg roups.com>,
xiey...@gmail.c om 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!

Jan 29 '07 #7
Xie Yubo wrote:
On Jan 29, 7:33 am, Jerry Coffin <jcof...@taeus. comwrote:
>In article <1170026540.624 521.97...@h3g20 00cwc.googlegro ups.com>,
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
Jan 29 '07 #8
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
Jan 29 '07 #9
On Jan 29, 5:32 pm, "Grizlyk" <grizl...@yande x.ruwrote:
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!

Jan 31 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
3695
by: gino | last post by:
Dear all, My monitor was set to 1600x1200 so the fonts in IE is too small for me even when I set the "View->Text Size->Largest"... I don't have previlage to change the monitor resolution... so I have to try to "hardcode" IE to display any webpage by a scale of 200%...
7
2913
by: jsale | last post by:
I'm currently using ASP.NET with VS2003 and SQL Server 2003. The ASP.NET app i have made is running on IIS v6 and consists of a number of pages that allow the user to read information from the database into classes, which are used throughout the application. I have made class collections which, upon reading from the DB, create an instance of the class and store the DB values in there temporarily. My problem is that if user1 looks at...
4
2510
by: Michael | last post by:
Dear all .. If I want to use develop a user control and declare a public property which the type is System.Windows.Forms.GridTableStylesCollection For example : Public Class LookAndView Inherits System.Windows.Forms.UserControl Private _Collection As GridTableStylesCollection
2
3335
by: Michael Schöller | last post by:
Hi I have some "unusual" problem ^^ Is there an way to enforce the declaration of values? If the answer is no use propertys can anyone tell me how to access a construct like this over properties. Access should be easy like ....some code Memory m; ....some code byte v = m.complete;
3
2004
by: Matt F. | last post by:
I have an abstract class that about a dozen sub-classes inherit from. I want to enforce that each sub-class shadows an event in the abstract class, but can't quite figure out how to do this. Basically, all of the inherited classes deal with data in some way or another. I need to make sure that when the data is changed on any of those classes, they raise an event called "DataChanged", and have that enforced in some way from the abstract...
3
1334
by: Wayne | last post by:
Are user-defined conversions chosen at compile time, or are there ways to make sure that they are chosen at run-time, based on the actual type of the object? Here is a simplified example of what I'm trying to accomplish: I have a method that has a parameter of type object. If the parameter is a boxed uint, it should unbox it. So, the method looks like this: void f(object o)
4
2102
by: zaeminkr | last post by:
I got a good answer here I have still confusing part. I have two very simple classes class DRect { private : double x0, y0, x1, y1; public : DRect(double a, double b, double c, double d) : x0(a), y0(b),
3
1988
by: mweltin | last post by:
I have been looking in the archives and as of yet have not found an answer to my problem. Class B has four members, and class A is derived from class B. Class A only adds one new member to class B. Class A has '<', '>', '==' and '=' overloaded, as well as a copy constructor. Class B only has the copy constructor and assignment operator overloaded. I create a vector of A objects, and stuff them with random values. When I sort the...
11
2186
by: jacob navia | last post by:
Hi Suppose that I want to create an array of read only items I overload the operator. How can I detect if I am being called within a read context foo = Array; or within a write context Array = foo;
0
8459
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8367
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8570
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7391
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6206
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5677
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4202
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2781
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2017
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.