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

Is it bad to use references in these cases?

P: n/a
Hi everyone,

A while ago I asked for help about using smart pointers (ie.
shared_ptr) in cyclic situations. Weak ptrs popped out and I gave it a
try, but to be honest, I don't feel very comfortable with them.

Would using references be a bad choice? The more I think about it the
more I realize it shouldn't be any problem with them, so could it be a
good use case?

As a refresher, this would be one situation where I'm planning to use
it:

class provider {
public:
provider(observer& obs) : obs_(obs) {}

private:
observer& obs_;
};

class creator : public observer
{
public:
creator() : provider_(new provider(*this))
{}

private:
shared_ptr<providerprovider_;
};

Where observer would be an abstract class with some defined pure
virtual functions..

Could someone tell me what advantage would have using weak pointers
instead of plain references?
I don't see any danger here, as the provider class will be destroyed
while the creator is being destroyed. So the provider can be sure that
creator will always exist. Any pitfall here?

Only thing that I don't like too much is using this in the ctor, but I
think this is safe to do in this particular case..

And besides (I'm deviating the discussion a bit here), using weaker
pointers wouldn't help too much in the hypothetic case that the (weak)
pointer became invalid, as we'd need to constantly check for nullity
which is quite a pain. I thought about using the null pattern in some
situations, but if I stick to references I wouldn't mind really..

I'd appreciate any comment from you experts.

Regards.

Sep 30 '07 #1
Share this Question
Share on Google+
1 Reply


P: n/a
On Sep 30, 4:22 am, sip.addr...@gmail.com wrote:
A while ago I asked for help about using smart pointers (ie.
shared_ptr) in cyclic situations. Weak ptrs popped out and I
gave it a try, but to be honest, I don't feel very comfortable
with them.
Understandably.
Would using references be a bad choice? The more I think about
it the more I realize it shouldn't be any problem with them,
so could it be a good use case?
If the value can never be null, and it need never change,
references are a possibility. Generally, however, for
navigation, raw pointers are still the best solution.
As a refresher, this would be one situation where I'm planning
to use it:
class provider {
public:
provider(observer& obs) : obs_(obs) {}
private:
observer& obs_;
};
class creator : public observer
{
public:
creator() : provider_(new provider(*this))
{}

private:
shared_ptr<providerprovider_;
};
Where observer would be an abstract class with some defined
pure virtual functions..
Could someone tell me what advantage would have using weak
pointers instead of plain references?
This looks like navigation: what's wrong with a raw pointer.
References as class members have two "disadvantages": they can't
be null, and they can't be changed (which makes implementing
assignment very difficult). If the fact that the "pointer"
can't be null is a pre-condition/invariant, and the class
doesn't support assignment, references are fine. Otherwise, raw
pointers may be the most appropriate solution.
I don't see any danger here, as the provider class will be
destroyed while the creator is being destroyed. So the
provider can be sure that creator will always exist. Any
pitfall here?
Only thing that I don't like too much is using this in the
ctor, but I think this is safe to do in this particular case..
And besides (I'm deviating the discussion a bit here), using
weaker pointers wouldn't help too much in the hypothetic case
that the (weak) pointer became invalid, as we'd need to
constantly check for nullity which is quite a pain. I thought
about using the null pattern in some situations, but if I
stick to references I wouldn't mind really..
I'd appreciate any comment from you experts.
If the pointer is only used for navigation, then a raw pointer
is all you need.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Oct 1 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.