overloading .

Discussion in 'C++' started by Kai-Uwe Bux, May 11, 2005.

  1. Kai-Uwe Bux

    Kai-Uwe Bux Guest

    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
     
    Kai-Uwe Bux, May 11, 2005
    #1
    1. Advertising

  2. Kai-Uwe Bux

    Alvin Guest

    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
     
    Alvin, May 11, 2005
    #2
    1. Advertising

  3. 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
     
    Victor Bazarov, May 11, 2005
    #3
  4. Kai-Uwe Bux

    Kai-Uwe Bux Guest

    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
     
    Kai-Uwe Bux, May 11, 2005
    #4
  5. 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
     
    Victor Bazarov, May 11, 2005
    #5
  6. Kai-Uwe Bux

    Kai-Uwe Bux Guest

    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
     
    Kai-Uwe Bux, May 12, 2005
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Iyer, Prasad C

    Overloading __init__ & Function overloading

    Iyer, Prasad C, Sep 30, 2005, in forum: Python
    Replies:
    3
    Views:
    6,430
    Fredrik Lundh
    Sep 30, 2005
  2. Fredrik Lundh
    Replies:
    0
    Views:
    457
    Fredrik Lundh
    Sep 30, 2005
  3. Steve Holden
    Replies:
    0
    Views:
    438
    Steve Holden
    Sep 30, 2005
  4. Iyer, Prasad C
    Replies:
    4
    Views:
    591
    John J. Lee
    Sep 30, 2005
  5. Fredrik Lundh
    Replies:
    0
    Views:
    412
    Fredrik Lundh
    Sep 30, 2005
Loading...

Share This Page