473,396 Members | 1,671 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,396 software developers and data experts.

smart pointer

Hello Experts!

I reading a book called programming with design pattern revealed
by Tomasz Muldner and here I read something that I don't understand so I ask
you.

First some background below is a class ResourceHandle that is a wrapper
class.
The copy constructor and the assignment operator are private to disallow
copying of the ResourcePointer
handle. The constructor associates the handle with the pointer to Resource
and the destructor deletes this pointer. Here I can use ResourceHandle
anywhere I can use ResourcePointer.
There is also a type conversion from ResourceHandle to ResourcePointer.

Now to my question what kind of expression is this
ResourceHandle r(newResource());
What I have found it's the same as I think.
Resource* r(new Resource());
But I'm not familar with that expression either.
Is this Resource* r(new Resource()); the same as
Resource* r = new Resource();

typedef Resource* ResourcePointer;
class ResourceHandle
{
public:
ResourceHandle(const ResourcePointer& r) : handle(r) {}

virtual ~ResourceHandle()
{ delete handle; }

operator ResourcePointer() const //type conversion from ResourceHandle
to ResourcePointer
{ return handle; }

private:
ResourcePointer handle;
ResourceHandle(const ResourceHandle&);
ResourceHandle% operator=(const ResourceHandle&);
};

Many thanks

//Tony
Aug 13 '05 #1
1 1238
Tony Johansson wrote:

Now to my question what kind of expression is this
ResourceHandle r(newResource());
nit pick - you meant this below, right ?
ResourceHandle r(new Resource());

ResouceHandle is a class that takes Resource* as a constructor argument.
i.e. it invokes

ResourceHandle::ResourceHandle(const Resource *& r)
What I have found it's the same as I think.
Resource* r(new Resource());
Yes - however, Resource* is a POD (plain old data) type and
ResourceHandle is a class.
But I'm not familar with that expression either.
Is this Resource* r(new Resource()); the same as
Resource* r = new Resource();
Yes - these are identical, especially for POD types.

In theory, you could write :

ResourceHandle r = new Resource();

In this case, the compiler would construct a temporary ResourceHandle
(using ResourceHandle::ResourceHandle(const Resource *& r) ) and then
assign it to r using :

ResourceHandle::ResourceHandle(const ResourceHandle&)

but in this case, this is illegal since this constructor is private.

typedef Resource* ResourcePointer;
class ResourceHandle
{
public:
ResourceHandle(const ResourcePointer& r) : handle(r) {}

virtual ~ResourceHandle()
{ delete handle; }

operator ResourcePointer() const //type conversion from ResourceHandle
to ResourcePointer
{ return handle; }

private:
ResourcePointer handle;
ResourceHandle(const ResourceHandle&);
ResourceHandle% operator=(const ResourceHandle&);
};

Many thanks

//Tony

Aug 13 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Mohammad | last post by:
Is there a difference between smart pointers and automatic pointers or they refer to the same idea. -Thanks
27
by: Susan Baker | last post by:
Hi, I'm just reading about smart pointers.. I have some existing C code that I would like to provide wrapper classes for. Specifically, I would like to provide wrappers for two stucts defined...
8
by: Axter | last post by:
I normally use a program call Doxygen to document my source code.(http://www.stack.nl/~dimitri/doxygen) This method works great for small and medium size projects, and you can get good...
92
by: Jim Langston | last post by:
Someone made the statement in a newsgroup that most C++ programmers use smart pointers. His actual phrase was "most of us" but I really don't think that most C++ programmers use smart pointers,...
14
by: Ian | last post by:
I am looking at porting code from a C++ application to C#. This requires implementing data sharing functionality similar to what is provided by a smart pointer in C++. I have only recently begun...
33
by: Ney André de Mello Zunino | last post by:
Hello. I have written a simple reference-counting smart pointer class template called RefCountPtr<T>. It works in conjunction with another class, ReferenceCountable, which is responsible for the...
10
by: =?iso-8859-1?q?Ernesto_Basc=F3n?= | last post by:
I am implementing my custom smart pointer: template <typename T> class MySmartPtr { public: MySmartPtr(T* aPointer) { mPointer = aPointer; }
4
by: Deep | last post by:
I'm in doubt about what is smart pointer. so, please give me simple description about smart pointer and an example of that. I'm just novice in c++. regards, John.
54
by: Boris | last post by:
I had a 3 hours meeting today with some fellow programmers that are partly not convinced about using smart pointers in C++. Their main concern is a possible performance impact. I've been explaining...
50
by: Juha Nieminen | last post by:
I asked a long time ago in this group how to make a smart pointer which works with incomplete types. I got this answer (only relevant parts included): ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
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...
0
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...
0
tracyyun
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...

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.