Thread: operator->

  1. #1
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490

    operator->

    Two questions...
    What does operator->* do? (and operator.* also)

    and

    Is it possible to overload the operator-> so that some function tacked on to the end of it is applied to a bunch of other objects?

    Ie:
    Code:
    template<class T> class Set {
      std::vector<T> members;
      //...other stuff
    };
    Set<Expression> s_e;
    s_e->combine();
    Where s_e->combine() would call that function of every 'T' in members?

  2. #2
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072

    Re: operator->

    Originally posted by ygfperson
    Two questions...
    What does operator->* do? (and operator.* also)
    To use pointer to members, you have to use those operators.
    Code:
    struct AStruct
    {
     int b,c;
    };
    AStruct obj;
    AStruct* pobj = &obj;
    int AStruct::* p = &AStruct::b;
    obj.*p = 2;
    p = &AStruct::c;
    pobj->*p = 3;
    Originally posted by ygfperson

    Is it possible to overload the operator-> so that some function tacked on to the end of it is applied to a bunch of other objects?

    Ie:
    Code:
    template<class T> class Set {
      std::vector<T> members;
      //...other stuff
    };
    Set<Expression> s_e;
    s_e->combine();
    Where s_e->combine() would call that function of every 'T' in members?
    You will need some kind of helper class that operator-> can return a pointer to.
    Code:
    template<typename T>
    class SetHelper
    {
      public:
    
      SetHelper(Set<T>& set)
      {
         mySet = set;
      }
    
      void combine()
      {
         //Call combine for every entry
      }
    
      protected: 
    
      Set<T>& mySet;
    };
    
    template<class T> class Set {
    ...
     Set()
     {
        seth = new SetHelper<T>( *this );
     }
    
     ~Set()
     {
        delete seth;
     }
    
    SetHelper<T>* operator->()
    {
      return seth;
    }
    
    SetHelper<T>* seth;
    I have certainly not tested this, it's just from the top of my head.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  3. #3
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    >> You will need some kind of helper class that operator-> can return a pointer to.

    I don't see why. Since 'members' is member data of 'Set', you could just define 'combine()' in 'Set', and use the default behavior of 'operator->'. Maybe I'm missing something though...
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  4. #4
    Registered User
    Join Date
    Jun 2003
    Posts
    70
    '->' is member access operator and
    '->*' is pointer to member access operator.

    These both operator can't be overload. because basically they take member name as argument.

    for example :

    struct ABC
    {
    int i;
    };

    code :
    ----------------
    ABC* abc;
    abc->i = 11;
    ----------------
    Chintan R Naik

  5. #5
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Actually, they can be overloaded. The only operators which cannot are: '.', '.*', and '::'.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  6. #6
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    You can't overload '?' or ':' either.

  7. #7
    Rad gcn_zelda's Avatar
    Join Date
    Mar 2003
    Posts
    942
    what's the '?' operator used for?

  8. #8
    Registered User
    Join Date
    Jun 2003
    Posts
    70
    Don't think '.' equals to '->' and '.*' equals to '->*' ?
    Chintan R Naik

  9. #9
    Registered User codegirl's Avatar
    Join Date
    Jun 2003
    Posts
    76
    Code:
    if (a == b)
      statement1;
    else
      statement2;
    is equivalent to

    Code:
    (a == b) ? statement1 : statement2;
    Lots of times it's used to save typing, such as:

    Code:
    if (a == b)
      cout << string1;
    else
      cout << string2;
    could be:

    Code:
    cout << ((a == b) ? string1 : string2);
    My programs don't have bugs, they just develop random features.

  10. #10
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    >> Don't think '.' equals to '->' and '.*' equals to '->*' ?

    I don't quite follow what your saying.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  11. #11
    Registered User
    Join Date
    Jun 2003
    Posts
    70

    -------------------------------------------------------------------------
    >> Don't think '.' equals to '->' and '.*' equals to '->*' ?

    I don't quite follow what your saying.
    -------------------------------------------------------------------------


    Sorry.......sorry....... I wanted to say :

    Don't you think '.' is conceptually equals to '->' and '.*' is conceptually equals to '->*' ???

    Both are used to access member of structure or class. So I want to say that if '.' can't be overload than '->' must not be overload.
    And similar with '.*' and '->*'.

    Thats all !!!!!!!!!
    Chintan R Naik

  12. #12
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Okay, got it.

    It makes sense that the . and .* operators cannot be overloaded. It is nice sometimes to overload -> though.

    Your right though, they do seem conceptually the same.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  13. #13
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    if operator -> couldn't be overloaded how could we have smart pointers???
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  14. #14
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    It'd be possible, but the syntax wouldn't be as nice.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  15. #15
    Registered User
    Join Date
    Jun 2003
    Posts
    70
    I have even read also in one C++ book that operator -> can't be overloaded.

    If you think that it can be overloaded, please give me some example if you can.
    Chintan R Naik

Popular pages Recent additions subscribe to a feed