Smart pointers: Conditional initialization

M

Matthias Kaeppler

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
 
A

Alf P. Steinbach

* 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 );
 
G

Gianni Mariani

Matthias said:
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 )
{}
};
 
J

John Harrison

Matthias said:
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
 
K

Kaz Kylheku

Matthias said:
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! :)
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top