473,394 Members | 1,715 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,394 software developers and data experts.

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 1265
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*********************@h3g2000cwc.googlegroups.c om>,
xi*****@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.
Jan 28 '07 #3


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

Jan 28 '07 #5
In article <11**********************@k78g2000cwa.googlegroups .com>,
xi*****@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.
Jan 29 '07 #6
On Jan 29, 7:51 am, Jerry Coffin <jcof...@taeus.comwrote:
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!

Jan 29 '07 #7
Xie Yubo wrote:
On Jan 29, 7:33 am, Jerry Coffin <jcof...@taeus.comwrote:
>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
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...@yandex.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
On 30 Jan 2007 21:37:52 -0800, "Xie Yubo" <xi*****@gmail.comwrote:
>On Jan 29, 5:32 pm, "Grizlyk" <grizl...@yandex.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
This is "half" of a Factory Method pattern. Why not go all the way?

class A
{
public:
static A& create();
static void destroy(A& a);

private:
A() {}
~A() {}
};

A& A::create() { return *new A; }
void A::destroy(A& a) { delete &a; }

int main()
{
A a; // error

A& ar = A::create();
A::destroy(ar);

return 0;
}

This pattern is also useful in case you want to encapsulate the creation
policy:
template <class CreationPolicy>
class A: private CreationPolicy
{
public:
static A& create();
static void destroy(A& a);

private:
typedef CreationPolicy Policy;
A() {}
~A() {}
};

A& A::create()
{
A* pa = Policy::allocate(sizeof(A));
new (pa) A();
return *pa;
}

/* ... */

int main()
{
A& ar0 = A<HeapCreationPolicy>::create();
A& ar1 = A<PoolCreationPolicy>::create();

/* ... */
}
Jan 31 '07 #11
Dave Rahardja wrote:
>
This is "half" of a Factory Method pattern. Why not go all the way?

Because he wants to use "new" and maybe hide stack protection from other
parts of code, are expecting "new" to work.
>Then the user can only create a C object with:
C* p = new C;
and
C c;
is forbided.
--
Maksim A Polyanin
Jan 31 '07 #12
I think there is a way to resolve your problem.

you can declare the constructor as private(if you don't want it can be
derived by other classes), or declare it as protected.
then, you add a static member function to call the constructor, I
think it will be better.

here is a example to show how to do this:

class C {
public:
static C* New();
~C();

protected:
C();
};

C* C::New() {
C *self = new C();
return self;
}

int main() {
C *pC = C::New();

// to destroy it, simply call delete.
delete pC;
pC = NULL;

return 0;
}

Feb 1 '07 #13

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

Similar topics

7
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... ...
7
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...
4
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...
2
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...
3
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. ...
3
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...
4
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) :...
3
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...
11
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...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
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...

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.