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
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