Smart Pointers and "new"

  • Thread starter Vijayaraghavan Kalyanapasupathy
  • Start date
V

Vijayaraghavan Kalyanapasupathy

Hello All,

I just learnt about smart pointers. Given the advantages of using
smart-pointers, as a designer of say a List object, I can inhibit
creation of the actual objects by making its constructors and then
making the smart pointer a friend of the class (I am talking about a
specific smart pointer implementation for a specific object). Now, it
seems to me that inhibiting the use of "new" with the smart pointer
would enhance things. For example, even if I created a smart pointer
and restricted all access to via the pointer, the user can still
create smart pointer objects themselves with new right? So, unless the
user deletes these explicitly, there is still a chance of leaks right?
I am sure others must have thought about it and so must have the
language designers. My question is, is it possible to inhibit dynamic
object creation via "new" for specific classes?

thanx,
vijai.
 
A

Alf P. Steinbach

* (e-mail address removed) (Vijayaraghavan Kalyanapasupathy) schriebt:
is it possible to inhibit dynamic object creation via "new" for
specific classes?

Yes, by making inaccessible something needed by "new" (constructor,
destructor, full definition of class).

Usually that means some factory pattern or a singleton.

The downside is that it tends to diminish the usefulness of the class,
and is much work for little gain.
 
J

Jeff Flinn

Vijayaraghavan Kalyanapasupathy said:
Hello All,

I just learnt about smart pointers. Given the advantages of using
smart-pointers, as a designer of say a List object, I can inhibit
creation of the actual objects by making its constructors and then
making the smart pointer a friend of the class (I am talking about a
specific smart pointer implementation for a specific object). Now, it
seems to me that inhibiting the use of "new" with the smart pointer
would enhance things. For example, even if I created a smart pointer
and restricted all access to via the pointer, the user can still
create smart pointer objects themselves with new right? So, unless the
user deletes these explicitly, there is still a chance of leaks right?
I am sure others must have thought about it and so must have the
language designers. My question is, is it possible to inhibit dynamic
object creation via "new" for specific classes?

See www.boost.org

class A : boost::noncopyable
{
int m1;

A();
A( int aArg1 ):m1(aArg1){}
public:
typedef boost::shared_ptr<A> tPtr;

static tPtr Make( int aArg1 ){ return tPtr( new A( aArg1 ) ); }

...
};

int main()
{
A::tPtr lAPtr = A::Make( 123 );

A* lRawAPtr = new A( 456 ); // fails
}

Jeff F
 

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,772
Messages
2,569,593
Members
45,112
Latest member
VinayKumar Nevatia
Top