By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,543 Members | 2,246 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,543 IT Pros & Developers. It's quick & easy.

Smart pointers: Conditional initialization

P: n/a
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
Nov 26 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
* 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?
Nov 26 '05 #2

P: n/a
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 )
{}
};
Nov 26 '05 #3

P: n/a
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
Nov 26 '05 #4

P: n/a
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! :)

Nov 27 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.