Thread: Overloading -> (and ->*)

  1. #1
    Massively Single Player AverageSoftware's Avatar
    Join Date
    May 2007
    Location
    Buffalo, NY
    Posts
    141

    Overloading -> (and ->*)

    I can't for the life of me figure out the correct way to overload the operators -> and ->*.

    I know that the standard library iterators do it, and that auto_ptr and various other smart pointer classes do it. I can't seem to find any useful documentation on the subject and was hoping someone here might be able to explain how to do this right.

    The first point of confusion is the parameter. I would think that the operator would take a member pointer, but that would necessitate overloading for every member function signature and member data type. This seems stupid and I doubt it is correct, but I can't think of anything else.

    Secondly, what should this operator return? Should it return anything at all? The return type of the function you invoke? Maybe the data type of the member you access?
    There is no greater sign that a computing technology is worthless than the association of the word "solution" with it.

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You could download and read "Thinking in C++", vol 1, chapter 12.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by AverageSoftware View Post
    Secondly, what should this operator return? Should it return anything at all? The return type of the function you invoke? Maybe the data type of the member you access?
    It returns a pointer to an object, upon which the built-in -> operator will act. For example, in a smart pointer implementation, the -> operator just returns the underlying pointer.

    In MOST cases your -> operator will simply return a pointer to some underlying object.

    EDIT: I suppose I should make this a bit clearer.

    operator-> takes NO parameters. Its only purpose is to return a pointer to some object, i.e. the object you are "emulating." In almost all cases where you are overloading ->, it's because you are writing some class B which wraps class A and protects or regulates it somehow (e.g., smart pointers, iterators, etc). So the operator-> will return a pointer to an object of class A, and class B protects or regulates access to this object.
    Last edited by brewbuck; 06-10-2007 at 11:54 AM.

  4. #4
    Massively Single Player AverageSoftware's Avatar
    Join Date
    May 2007
    Location
    Buffalo, NY
    Posts
    141
    So essentially, you're looking at something like this?

    Code:
    template <typename t>
    class PointerWrapper
    {
        t *WrappedPointer;
    
    public:
        t  *operator -> () { return WrappedPointer; }
    };
    I'm guessing then that -> and ->* can't be overloaded to do crazy unrelated stuff, since the compiler is depending on getting a pointer to an object returned.

    Thanks for clearing that up.
    There is no greater sign that a computing technology is worthless than the association of the word "solution" with it.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    They can be overloaded to do crazy stuff. It is not the compiler that expects certain behavior, it is the users of the class. The users of the class will expect the pointer to be returned and if other stuff happens then the operator won't work with their code. That's why it is generally a good idea to follow common practice when overloading operators. The compiler doesn't care until a user uses it differently than you intend.

Popular pages Recent additions subscribe to a feed