473,320 Members | 2,083 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Is it bad to use references in these cases?

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
1 1380
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Tom | last post by:
The motivation for references seems clear: stop people from using nasty pointers when all they really want is a reference to an object. But C++ references are so inadequate that I'm still using...
11
by: Daniel Wilcox | last post by:
I have a question, I have been given a piece of code that apparantly compiles under Visual C++ but I cannot get to compile under g++ 3.2. I have read the FAQ and delved into the Stroustrup book...
37
by: Dave | last post by:
Hello all, Please consider the code below. It is representative of a problem I am having. foo_t needs to contain a bar_t which is a class without a copy constructor or operator=. It is not...
25
by: Steve Jorgensen | last post by:
Yup, Steve's full of tips, but hey, it makes him feel important, right? Ok, here goes. I've been trying to improve encapsulation by putting code in the same object as the stuff it affects, so I...
0
by: ChipOne | last post by:
I have a Widget Class with a Parent and Child property of type Widget. When you populate a set of Widgets, you set references to the Parent and Child. I was using binary serialization, which...
7
by: Bob | last post by:
It's great that VS.NET makes it so effortless to add a web reference to a web service. The problem is, I haven't figured out a way to configure the URLs (or simply switch the references to another...
3
by: Jerad Rose | last post by:
This is regarding Visual Studio 2003 (framework 1.1). We have several projects/libraries. Of course, many of these reference each other. If we only had one solution, we would simply add all of...
11
by: =?iso-8859-1?q?Erik_Wikstr=F6m?= | last post by:
struct foo { int i; }; int bar(foo& f) { return f.i++; } int main() { bar(foo());
3
by: Tim | last post by:
Hi, I've been struggling for a while trying to run plugins in C#, which have external references. The plugins load, and I can invoke methods through reflection until the method uses some code...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.