Smart Pointers and "new"

Discussion in 'C++' started by Vijayaraghavan Kalyanapasupathy, Feb 24, 2004.

  1. 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.
     
    Vijayaraghavan Kalyanapasupathy, Feb 24, 2004
    #1
    1. Advertising

  2. * (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.
     
    Alf P. Steinbach, Feb 24, 2004
    #2
    1. Advertising

  3. Vijayaraghavan Kalyanapasupathy

    Jeff Flinn Guest

    "Vijayaraghavan Kalyanapasupathy" <> wrote in
    message news:...
    > 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
     
    Jeff Flinn, Feb 24, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. MotoK
    Replies:
    59
    Views:
    1,922
    Keith Thompson
    Sep 15, 2006
  2. Matthias Kaeppler
    Replies:
    5
    Views:
    464
    Axter
    Sep 10, 2005
  3. n2xssvv g02gfr12930

    Smart pointers and member function pointers

    n2xssvv g02gfr12930, Nov 26, 2005, in forum: C++
    Replies:
    3
    Views:
    502
    n2xssvv g02gfr12930
    Nov 27, 2005
  4. Steven T. Hatton
    Replies:
    7
    Views:
    406
    Kai-Uwe Bux
    Nov 30, 2006
  5. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    738
Loading...

Share This Page