Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > overloading .

Reply
Thread Tools

overloading .

 
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      05-11-2005
Hi folks,

I have a smart pointer class that guarantees the existence of a valid
pointee. Hence it does not really feel like a pointer (e.g., it would be
pointless to use it as a next-pointer in a list or tree structure). So I
would like to remove the pointer-likeness from the class interface. Thus, I
am wondering if it is possible to make a smart pointer that does not look
like a pointer. Here is a basic idea:

#include <iostream>

template < typename T >
class dummy {
private:

T * ptr;

public:

dummy ( T const & t = T() )
: ptr ( new T ( t ) )
{}

operator T & ( void ) {
return( *ptr );
}

operator T const & ( void ) const {
return( *ptr );
}

}; // named_class

struct T {

int k;

};

int main ( void ) {
dummy< int > i ( 5 );
i = 6; // works
std::cout << i << '\n'; // works
dummy<T> t;
t.k = 5; // COMPILE TIME ERROR
}

I would like to define the template dummy<> in such a way that this
compiles and delivers the expected results, i.e., I would like to overload
"operator." if there was such a thing. Any ideas as to how this could be
done (or any ideas how to prove it impossible) would be appreciated.


Thanks

Kai-Uwe Bux
 
Reply With Quote
 
 
 
 
Alvin
Guest
Posts: n/a
 
      05-11-2005
Kai-Uwe Bux wrote:

> Hi folks,
>
> I have a smart pointer class that guarantees the existence of a valid
> pointee. Hence it does not really feel like a pointer (e.g., it would be
> pointless to use it as a next-pointer in a list or tree structure). So I
> would like to remove the pointer-likeness from the class interface. Thus,
> I am wondering if it is possible to make a smart pointer that does not
> look like a pointer. Here is a basic idea:
>

snip
>
> int main ( void ) {
> dummy< int > i ( 5 );
> i = 6; // works
> std::cout << i << '\n'; // works
> dummy<T> t;
> t.k = 5; // COMPILE TIME ERROR
> }
>
> I would like to define the template dummy<> in such a way that this
> compiles and delivers the expected results, i.e., I would like to overload
> "operator." if there was such a thing. Any ideas as to how this could be
> done (or any ideas how to prove it impossible) would be appreciated.


This doesn't answer your questions, but it looks like you need to overload
the operator*, so the line in question would look like:

(*t).k = 5;

I don't think there is such a thing as operator.()

--
Alvin
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      05-11-2005
Alvin wrote:
> [...]
> I don't think there is such a thing as operator.()


There is. It's not overloadable. There are three other operators that
are listed as non-overloadable: member access through pointer to member
for objects (.*), scope resolution (: and ternary (?. Of course,
'sizeof' is not overloadable either.

V
 
Reply With Quote
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      05-11-2005
Victor Bazarov wrote:

> Alvin wrote:
>> [...]
>> I don't think there is such a thing as operator.()

>
> There is. It's not overloadable. There are three other operators that
> are listed as non-overloadable: member access through pointer to member
> for objects (.*), scope resolution (: and ternary (?. Of course,
> 'sizeof' is not overloadable either.


Thanks, I found the clause in the standard that says operator. cannot be
overloaded. Too bad.

Anyway, do you know of a work-around that would solve my original problem
of making dummy<T> behave like it provided the same interface as T although
all actual calls would be forwarded to a pointee of type T?


Best

Kai-Uwe Bux
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      05-11-2005
Kai-Uwe Bux wrote:
> Victor Bazarov wrote:
>
>
>>Alvin wrote:
>>
>>>[...]
>>>I don't think there is such a thing as operator.()

>>
>>There is. It's not overloadable. There are three other operators that
>>are listed as non-overloadable: member access through pointer to member
>>for objects (.*), scope resolution (: and ternary (?. Of course,
>>'sizeof' is not overloadable either.

>
>
> Thanks, I found the clause in the standard that says operator. cannot be
> overloaded. Too bad.
>
> Anyway, do you know of a work-around that would solve my original problem
> of making dummy<T> behave like it provided the same interface as T although
> all actual calls would be forwarded to a pointee of type T?


Nothing elegant, I'm afraid. I once wrote a "reference" class (similar to
what you're doing), and, IIRC, used the unary + operator to return the
reference to the object, which in your terms would look like

dummy<T> t;
(+t).k = 5;

(Not much better than (*t).k, if you ask me)

I'd say, if you need things done, use a smart_ptr or shared_ptr, and don't
bother with the operator dot.

V
 
Reply With Quote
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      05-12-2005
Victor Bazarov wrote:

> Kai-Uwe Bux wrote:
>> Victor Bazarov wrote:

[snip]
>> Anyway, do you know of a work-around that would solve my original problem
>> of making dummy<T> behave like it provided the same interface as T
>> although all actual calls would be forwarded to a pointee of type T?

>
> Nothing elegant, I'm afraid. I once wrote a "reference" class (similar to
> what you're doing), and, IIRC, used the unary + operator to return the
> reference to the object, which in your terms would look like
>
> dummy<T> t;
> (+t).k = 5;
>
> (Not much better than (*t).k, if you ask me)
>
> I'd say, if you need things done, use a smart_ptr or shared_ptr, and don't
> bother with the operator dot.


Thanks again. I will go with the pointer-like syntax for the time being.
Although it feels somewhat odd that C++ offers all kinds of nice syntactic
suggar and yet does not seem to have a way to fake inheritance or to
forward in interface.


Best

Kai-Uwe Bux
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
RE: Overloading __init__ & Function overloading Iyer, Prasad C Python 4 09-30-2005 08:01 PM
Re: Overloading __init__ & Function overloading Fredrik Lundh Python 0 09-30-2005 03:59 PM
Overloading __init__ & Function overloading Iyer, Prasad C Python 3 09-30-2005 02:17 PM
Re: Overloading __init__ & Function overloading Steve Holden Python 0 09-30-2005 01:58 PM
Re: Overloading __init__ & Function overloading Fredrik Lundh Python 0 09-30-2005 01:53 PM



Advertisments