Hello,
Given something like:
boost::shared_p tr<T> t( new T() );
What is the best (correct?) way to dereference the pointer? The following
two methods work. Is there a difference?
T &rt1 = *t.get();
T &rt2 = *t;
And what about getting at the raw pointers:
T *pt1 = t.get();
T *pt2 = &*t;
Is there an advantage to one or the other?
Thanks,
- Dennis 2 7231
"Dennis Jones" <no****@nospam. com> wrote in message
news:11******** *****@corp.supe rnews.com... Hello,
Given something like:
boost::shared_p tr<T> t( new T() );
What is the best (correct?) way to dereference the pointer? The following two methods work. Is there a difference?
T &rt1 = *t.get(); T &rt2 = *t;
I prefer the second method, because you get to type four fewer characters
:-) Also, suppose you have a typedef somewhere that looks like
typedef shared_ptr<T> TPtr;
In the second case, you maintain the option of changing this to
typedef T *TPtr;
without having to change any dereferencing code.
And what about getting at the raw pointers:
T *pt1 = t.get(); T *pt2 = &*t;
I definitely prefer the first case here. What is t.get() == 0. Then the
second case invokes undefined behavior by dereferencing a null pointer. For
example, the boost implementation of shared_ptr asserts that get() != 0
inside its implementations of operator*() and operator->().
Joe Gottman
"Joe Gottman" <jg******@carol ina.rr.com> wrote in message
news:Vb******** *************@t wister.southeas t.rr.com... What is the best (correct?) way to dereference the pointer? The
following two methods work. Is there a difference?
T &rt1 = *t.get(); T &rt2 = *t;
I prefer the second method, because you get to type four fewer characters :-) Also, suppose you have a typedef somewhere that looks like typedef shared_ptr<T> TPtr;
Thank you. I agree with your assessment. I was just a little surprised
that the second one compiled and worked. I thought (perhaps naively) it
looked like I was dereferencing the shared_ptr instead of the underlying raw
pointer. But now that I look at the header file, I see that operator *() is
defined to return a reference to the pointee. And what about getting at the raw pointers:
T *pt1 = t.get(); T *pt2 = &*t;
I definitely prefer the first case here. What is t.get() == 0. Then
the second case invokes undefined behavior by dereferencing a null pointer.
For example, the boost implementation of shared_ptr asserts that get() != 0 inside its implementations of operator*() and operator->().
I wouldn't have thought of that -- you're absolutely right.
Thanks for your insight,
- Dennis This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Philippe Guglielmetti |
last post by:
I just ported old (VC6) working code to VC7.1 and have trouble with
something like:
class A; // forward
typedef boost::smart_ptr<A> Aptr;
class B{
Aptr a;
virtual ~B(); // implemented after A has been defined
};
|
by: pub |
last post by:
When creating a list:
list<class A*> l;
How to delete all the objects whose pointers are contained in "l"?
Thanks for your comments?
|
by: Tommy Vercetti |
last post by:
I'm new to Managed C++ development, and I can't seem to get started.
Every time I try to include vcclr.h I get bizarre compilation errors.
I've isolated this to a really simple case:
In Visual Studio .NET 2003, I do the following
1) File->New->Project
2) Visual C++ Projects->.NET->Windows Forms Application
3) Project->Add New Item->C++ File
4) Replace the contents of the new C++ file with the simple lines:
#include "stdafx.h"
|
by: Marcus Kwok |
last post by:
Hello, I am working on cleaning up some code that I inherited and was
wondering if there is anything wrong with my function. I am fairly
proficient in standard C++ but I am pretty new to the .NET managed C++.
It seems to work fine, but everyone knows that programs with errors can
still appear to "work fine" :)
I am working with VS .NET 2003; I am unable to upgrade to 2005 at this
time, so I cannot use the newer syntax or features.
...
|
by: google |
last post by:
Hi All,
I'm just getting started learning to use <algorithm> instead of loads
of little for loops, and I'm looking for a bit of advice/mentoring re:
implementing the following...
I have a vector of boost::shared_ptr's, some of which may be NULL. I'd
like to find out if there are any non-NULL elements in the vector.
Here's what I have at the moment.
| |
by: Colin Caughie |
last post by:
Is there a general rule/convention for when to use angle brackets and
when to use quotes in #include statements? Is the angle bracket reserved
for "system" header files (e.g. standard library headers), or is it for
general "stable" include files?
What about the grey areas in between these and actual application code,
e.g. headers for a library that is under my control but external to the
project being compiled?
I know both styles...
|
by: Martin |
last post by:
Hi
I need to maintain a <setof pointers to objects, and it must be
sorted based on the values of pointed objects, not pointer values. I
can achieve this easily by defining my own comparing predicate for the
<set>. Here is an example:
#include <string>
#include <set>
using namespace std;
|
by: Emmanuel Deloget |
last post by:
Hello y'all,
I'm currently writing a serie of blog tickets about the TR1, both from
a definition point of view and from an implementation point of view.
The next iteration in the series deals with the smart pointer sub-
library in the TR1 (weak_ptr, shared_ptr, enable_shared_from_this),
and guess what, I have a few question about the implementation of all
this.
Before I ask my question, I'd like to say that I studied all the
|
by: =?Utf-8?B?RmFiaWFu?= |
last post by:
Hello,
I have a class hierarchy distributed over 3 native C++ dlls. The base class
has a .NET Windows.Form for status output via a gcroot<>.
The gcroot is declared private - the sub classes only have access via a
protected "print"-method.
I need the different dlls as the sub classes implement the base class's pure
virtual methods using different technologies.
To use the native classes from outside their dlls I use the...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |