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

X

Xie Yubo

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!
 
I

Ian Collins

Xie said:
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; }
};
 
X

Xie Yubo

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;
 
X

Xie Yubo

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;
 
J

Jerry Coffin

[ ... ]
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.
 
X

Xie Yubo

[ ... ]
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!
 
C

Chris

Xie said:
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.
 
G

Grizlyk

Xie said:
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.
 
X

Xie Yubo

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.

Wonderful! Thank you very much!
 
D

Dave Rahardja

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();

/* ... */
}
 
G

Grizlyk

Dave said:
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.
 
R

Rossonera

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;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top