Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Smart Pointers and "new"

Reply
Thread Tools

Smart Pointers and "new"

 
 
Vijayaraghavan Kalyanapasupathy
Guest
Posts: n/a
 
      02-24-2004
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.
 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      02-24-2004
* http://www.velocityreviews.com/forums/(E-Mail 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.

 
Reply With Quote
 
 
 
 
Jeff Flinn
Guest
Posts: n/a
 
      02-24-2004

"Vijayaraghavan Kalyanapasupathy" <(E-Mail Removed)> wrote in
message news:(E-Mail Removed) m...
> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
pointers, pointers, pointers... cerr C Programming 12 04-07-2011 11:17 PM
Smart Pointers: Is there something similar to smart pointers in C? MotoK C Programming 59 09-15-2006 07:03 PM
Smart pointers and member function pointers n2xssvv g02gfr12930 C++ 3 11-27-2005 10:51 AM
Smart Card Certificate Logon and Smart Card Wireless EAP-TLS erha Wireless Networking 0 05-19-2005 01:40 AM
trade 64mb smart media for 16mb smart media cards wjva Digital Photography 1 08-20-2003 08:30 PM



Advertisments