473,789 Members | 3,084 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Using the () operator for dereferencing a smart pointer

Hello,

We have a smart pointer class which provides the dereference
operator -> to access the underlying object pointer. Now, we have a new
requirement where a different type of object (e.g from memory, disk,
network etc) needs to be returned by the smart pointer on access.

I was thinking of using the function call operator () since that can
take arguments e.g :-

RefPtr(FROM_DIS K)->getAttr1()
RefPtr(FROM_CAC HE)->getAttr1().

Seeking your opinion on any issues/pitfalls? Is there any more
expressive solution?

Thanks.

Jul 23 '05 #1
6 3098
* Shankar:
We have a smart pointer class which provides the dereference
operator -> to access the underlying object pointer. Now, we have a new
requirement where a different type of object (e.g from memory, disk,
network etc) needs to be returned by the smart pointer on access.

I was thinking of using the function call operator () since that can
take arguments e.g :-

RefPtr(FROM_DIS K)->getAttr1()
RefPtr(FROM_CAC HE)->getAttr1().

Seeking your opinion on any issues/pitfalls? Is there any more
expressive solution?


As described it seems to be an UnGood design, and then the technical
"solution" to realize the design doesn't matter; UnGood is UnGood.

A smart pointer's primary responsibility is to manage the lifetime of an
underlying object and provide access to that object. It might have
selectable policies related to lifetime management (e.g. reference counted,
intrusive) and object access (e.g. threading policy). What it doesn't do is
to manage a collection of different objects (that's better solved by having
the smart pointer manage a collection object), and furthermore, leaving it
up to the client code to select the "right" object from a collection of
objects representing conceptually the same, at any given time such an object
is needed, is probably a recipe for disaster.

If it's impossible to make the storage policy of the underlying object
transparent, i.e. the client code needs to know and use aspects of that
policy and treat different kinds of objects differently, then I suggest
using different types for different kinds of objects. That's what types are
for. If, on the other hand, there's nothing that different types can
contribute to the client code, the client code treats all such objects alike
as if of the same type, then use the same type and hide the storage policy.

--
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?
Jul 23 '05 #2
Shankar wrote:
Hello,

We have a smart pointer class which provides the dereference
operator -> to access the underlying object pointer. Now, we have a new
requirement where a different type of object (e.g from memory, disk,
network etc) needs to be returned by the smart pointer on access.

I was thinking of using the function call operator () since that can
take arguments e.g :-

RefPtr(FROM_DIS K)->getAttr1()
RefPtr(FROM_CAC HE)->getAttr1().

Seeking your opinion on any issues/pitfalls? Is there any more
expressive solution?

If you're using the smart pointer to manage the caching then just
reassigning the pointer from the pointer to the most recent copy
should work, e.g.

RefPtr = globalPtr(); // get lastest disk version

--
Joe Seigh

When you get lemons, you make lemonade.
When you get hardware, you make software.
Jul 23 '05 #3

"Shankar" <sh******@yahoo .co.in> wrote in message
news:11******** *************@o 13g2000cwo.goog legroups.com...
Hello,

We have a smart pointer class which provides the dereference
operator -> to access the underlying object pointer. Now, we have a new
requirement where a different type of object (e.g from memory, disk,
network etc) needs to be returned by the smart pointer on access.

I was thinking of using the function call operator () since that can
take arguments e.g :-

RefPtr(FROM_DIS K)->getAttr1()
RefPtr(FROM_CAC HE)->getAttr1().

Seeking your opinion on any issues/pitfalls? Is there any more
expressive solution?

Thanks.


It really doesn't go with the pointer semantics. And in such situation you
always have a better choice to make everything explicit. Use ordinary
functions or member functions:

template <typename T>
T* get_from_disk() ;

template <typename T>
T* get_from_cache( );

I don't quite get what you want to achieve but I hope this might be a little
helpful.

Regards,
Ben
Jul 23 '05 #4
Shankar wrote:
We have a smart pointer class which provides the dereference
operator -> to access the underlying object pointer. Now, we have a new
requirement where a different type of object (e.g from memory, disk,
network etc) needs to be returned by the smart pointer on access.

I was thinking of using the function call operator () since that can
take arguments e.g :-

RefPtr(FROM_DIS K)->getAttr1()
RefPtr(FROM_CAC HE)->getAttr1().


The C++ language is, after all, a language. Would others understand
what you are trying to say?

Jul 23 '05 #5
I am wondering if a better design might not be to use different types
for the different functionality (disk, cache etc) and require them to
implement the same interface. The smart pointer can store a pointer to
the interface. Virtual inheritance should solve the issue of invoking
the appropriate methods. Here goes:

class ProtectedObject
{
public:
virtual void foo() = 0;
virtual void bar() = 0;
};

class Disk : public ProtectedObject
{
};

class Cache : public ProtectedObject
{
};

class SmartPointer
{
friend class Disk;
friend class Cache;
public:
ProtectedObject * operator->();
protected:
SmartPointer(Pr otectedObject*) ;
~SmartPointer() ;
private:
ProtectedObject * m_object;
};

-vijai.

Jul 23 '05 #6

Shankar wrote:
Hello,

We have a smart pointer class which provides the dereference
operator -> to access the underlying object pointer. Now, we have a new
requirement where a different type of object (e.g from memory, disk,
network etc) needs to be returned by the smart pointer on access.

I was thinking of using the function call operator () since that can
take arguments e.g :-

RefPtr(FROM_DIS K)->getAttr1()
RefPtr(FROM_CAC HE)->getAttr1().

Seeking your opinion on any issues/pitfalls? Is there any more
expressive solution?

Thanks.

Not to put too fine a point on it, but overloading the function call
operator for this purpose is a truly terrible idea, guaranteed to cause
no end of side effects and confusion between the () operator and
parentheses that are being used for other reasons.

I think that a sensible approach is to have global accessor functions
that extract the desired information from the passed-in values. For
example, implementing GetDiskPtr() and GetCachePtr() helper functions
like this::

GetDiskPtr( refPtr )->GetAttr1();
GetCachePtr( refPtr )->GetAttr1();

I do have one more suggestion: Try to use complete words (or well-known
acroynyms) in function names. "GetAttr1() " conveys little to me about
the purpose of the routine and even less to help me know when to call
it instead of GetAttr2(). And unless your compiler vendor charges you
by each letter in a routine's name, I see little downside in more
descriptive names. True, longer names may take a little more time to
type - but contrary to the thinking in some circles, their presence is
unlikely to slow the program down. :)

Greg

Jul 23 '05 #7

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

Similar topics

18
9724
by: Joe Seigh | last post by:
Is there a good write on this. The textbooks I have fluff over on this? Specifically, I trying to dereference with 2 levels of type conversion not 1, i.e. X<T> -> z => Y<T> -> z => T* -> z and *X<T> => *Y<T> => *T The Y<T> conversion has to be done as an expression temp. It cannot be done
10
2276
by: Tony Johansson | last post by:
Hello Experts!! This class template and main works perfectly fine. I have this class template called Handle that has a pointer declared as T* body; As you can see I have a reference counter in the class template so I know how many references I have to the body. In my case it's the Integer wrapper class which is the body. This template works for many different types. The one that I use is named Integer and is a Wrapper for an int. The...
24
2279
by: Bangalore | last post by:
Hi all, I have a problem in accessing elements using overloaded operator . Consider, const int SIZE=10; int FALSE=0; class Array { private: int x; public:
3
385
by: md | last post by:
Hi, the following code is working for static objects. ie the statement IntArray x(20); my problem is i want to use this overloading operator for dynamically created objects for example the statement IntArray *x; Please give me some help regarding this ////////////////////////////////////////////////////////////////////////////////////////////////////////// class IntArray
2
1585
by: mati-006 | last post by:
Hi Why there is no "counted_ptr& operator= (pointee_type* p)" ? This question has arisen when I was searching why following piece of code won't compile: arg::counted_ptr<testc; c = new test; And compiler wasn't very helpful with this:
10
2967
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; }
19
2515
by: Rajesh S R | last post by:
Consider the following code: int main() { struct name { long a; int b; long c; }s={3,4,5},*p; p=&s;
68
4656
by: Jim Langston | last post by:
I remember there was a thread a while back that was talking about using the return value of a function as a reference where I had thought the reference would become invalidated because it was a temporary but it was stated that it would not. This has come up in an irc channel but I can not find the original thread, nor can I get any code to work. Foo& Bar( int Val ) { return Foo( Val ); }
3
8293
by: Nathan | last post by:
Hi, I'm trying to do some network programming, but when I use 'struct addrinfo' or getaddrinfo, I see errors: gcc --std=c99 -Wall ipv6_test.c -o it ipv6_test.c: In function `main': ipv6_test.c:25: error: storage size of 'hints' isn't known ipv6_test.c:34: warning: implicit declaration of function `getaddrinfo' ipv6_test.c:38: error: dereferencing pointer to incomplete type
0
9511
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10408
Oralloy
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...
0
10199
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 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...
1
10139
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,...
0
9983
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 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...
0
9020
agi2029
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...
0
6768
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5417
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...
2
3697
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.