overloading .

K

Kai-Uwe Bux

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
 
A

Alvin

Kai-Uwe Bux said:
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.()
 
V

Victor Bazarov

Alvin said:
[...]
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
 
K

Kai-Uwe Bux

Victor said:
Alvin said:
[...]
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
 
V

Victor Bazarov

Kai-Uwe Bux said:
Victor Bazarov wrote:

Alvin said:
[...]
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
 
K

Kai-Uwe Bux

Victor said:
Kai-Uwe Bux said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,821
Latest member
AleidaSchi

Latest Threads

Top