473,505 Members | 14,252 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_DISK)->getAttr1()
RefPtr(FROM_CACHE)->getAttr1().

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

Thanks.

Jul 23 '05 #1
6 3079
* 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_DISK)->getAttr1()
RefPtr(FROM_CACHE)->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_DISK)->getAttr1()
RefPtr(FROM_CACHE)->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*********************@o13g2000cwo.googlegro ups.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_DISK)->getAttr1()
RefPtr(FROM_CACHE)->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_DISK)->getAttr1()
RefPtr(FROM_CACHE)->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(ProtectedObject*);
~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_DISK)->getAttr1()
RefPtr(FROM_CACHE)->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
9666
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* ->...
10
2250
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...
24
2231
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...
2
1551
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; ...
10
2936
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
2479
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
4551
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...
3
8248
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':...
0
7216
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
7098
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
7367
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...
1
5028
isladogs
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...
0
4699
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...
0
3187
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...
0
1528
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 ...
1
754
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
407
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.