Smart pointers: Conditional initialization

Discussion in 'C++' started by Matthias Kaeppler, Nov 26, 2005.

  1. Hi,

    I'm having a hard time figuring out how I can initialize a smart pointer
    based on a certain condition:

    if something then
    ptr = 0; // init with NULL
    else
    ptr = new XYZ; // init with a sane value
    endif

    I know assignment doesn't work for a smart pointer unless the assigned
    value is a smart pointer itself.

    Any ideas how I can trick the compiler to do what I need? The problem is
    simply that smart pointers expect the pointee to be passed to their
    ctor, but the ctor is only called once.

    Regards,
    Matthias Kaeppler
     
    Matthias Kaeppler, Nov 26, 2005
    #1
    1. Advertising

  2. * Matthias Kaeppler:
    >
    > I'm having a hard time figuring out how I can initialize a smart pointer
    > based on a certain condition:
    >
    > if something then
    > ptr = 0; // init with NULL
    > else
    > ptr = new XYZ; // init with a sane value
    > endif


    If this a "next"-pointer in a linked list, or some such, then maybe.

    Otherwise having a potential nullpointer around only leads to problems.

    But it's simple to shoot yourself in the foot, if you so want:

    SmartPtr ptr( condition? new XYZ : 0 );


    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Nov 26, 2005
    #2
    1. Advertising

  3. Matthias Kaeppler wrote:
    > Hi,
    >
    > I'm having a hard time figuring out how I can initialize a smart pointer
    > based on a certain condition:
    >
    > if something then
    > ptr = 0; // init with NULL
    > else
    > ptr = new XYZ; // init with a sane value
    > endif
    >
    > I know assignment doesn't work for a smart pointer unless the assigned
    > value is a smart pointer itself.
    >
    > Any ideas how I can trick the compiler to do what I need? The problem is
    > simply that smart pointers expect the pointee to be passed to their
    > ctor, but the ctor is only called once.


    It depends on the type of smart pointer you're using.

    Somthing like this might work.

    struct X
    {
    smartptr z;
    X()
    : x( a ? 0 : new Y )
    {}
    };
     
    Gianni Mariani, Nov 26, 2005
    #3
  4. Matthias Kaeppler wrote:
    > Hi,
    >
    > I'm having a hard time figuring out how I can initialize a smart pointer
    > based on a certain condition:
    >
    > if something then
    > ptr = 0; // init with NULL
    > else
    > ptr = new XYZ; // init with a sane value
    > endif
    >
    > I know assignment doesn't work for a smart pointer unless the assigned
    > value is a smart pointer itself.
    >
    > Any ideas how I can trick the compiler to do what I need? The problem is
    > simply that smart pointers expect the pointee to be passed to their
    > ctor, but the ctor is only called once.
    >
    > Regards,
    > Matthias Kaeppler


    Surely this works

    if something then
    ptr = SmartPtr(0); // init with NULL
    else
    ptr = SmartPtr(new XYZ); // init with a sane value

    where SmartPtr is your smart pointer class.

    Alternatively you could write a function returning a regular pointer, or
    you could use the conditional operator, but the above seems the most
    general solution.

    john
     
    John Harrison, Nov 26, 2005
    #4
  5. Matthias Kaeppler

    Kaz Kylheku Guest

    Matthias Kaeppler wrote:
    > I know assignment doesn't work for a smart pointer unless the assigned
    > value is a smart pointer itself.


    In addition the other answer, one obvious one is this: fix the smart
    pointer class so that you can assign regular pointers to it which
    become owned.

    If you are using auto_ptr, the reset() function does that:

    auto_ptr<char> x;

    x.reset(new char [20]);

    It's ugly, but that's what you get when a committee finally agrees on
    something something: a bastard child with a dozen fathers.

    A decent smart pointer class has operators that let it interoperate
    smoothly with dumb pointers, such as a T * operator so that the smart
    pointer objects can appear to be directly passed into functions that
    expect T * pointers, equality/inequality operators so that smart
    pointers can be copmared with dumb pointers, an assignment operator so
    that a dumb pointer can be imported easily and becomes owned, etc.

    Remember, even if the class designer didn't provide some operator, you
    can write one yourself outside of the class, since operators can be
    non-member functions:

    template <class T>
    poor_smart_ptr<T> &operator = (poor_smart_ptr<T> &lhs, T *rhs)
    {
    lhs = poor_smart_ptr<T>(rhs);
    return lhs;
    }

    There, now you can assign T * pointers to a poor_smart_ptr<T> object.

    See? NON-membership can have its privileges too! :)
     
    Kaz Kylheku, Nov 27, 2005
    #5
    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,817
    Keith Thompson
    Sep 15, 2006
  2. n2xssvv g02gfr12930

    Smart pointers and member function pointers

    n2xssvv g02gfr12930, Nov 26, 2005, in forum: C++
    Replies:
    3
    Views:
    472
    n2xssvv g02gfr12930
    Nov 27, 2005
  3. Boris
    Replies:
    5
    Views:
    1,075
    Fei Liu
    Apr 19, 2006
  4. cerr

    pointers, pointers, pointers...

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

Share This Page