Query regarding Overloading Address-of Operator

This is a discussion on Query regarding Overloading Address-of Operator within the C++ Programming forums, part of the General Programming Boards category; Hi Folks, Why doesn't overloading the Address-Of Operator result in a recursion??? Code: class X { public: X* operator&() // ...

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    103

    Query regarding Overloading Address-of Operator

    Hi Folks,

    Why doesn't overloading the Address-Of Operator result in a recursion???

    Code:
    class X {
    
         public:
         
         X* operator&()    // Compiler internally should convert it as X* operator&(X* const this)
         {
              cout << " Why does it not result in a recursive call??? " << endl;
              return this;
         }
    };
    
    int main()
    {
         X ob;
         X *prt = &ob; 
         // implies ==> X* ptr = ob.X::operator&() 
        //  which implies ==> X* prt = X::operator&(&ob);
    
         // My question is since the call to operator& member function must pass the 
        // address of the calling object, logically shouldn't this result in a recursive call.
    
        // I would like to understand how does it work ??? or rather why it works
    }
    regards,
    Shiv
    Have a wonderful day.... and keep smiling... you look terrific that way
    signing off...
    shiv... as i know him

  2. #2
    Registered User
    Join Date
    Jun 2004
    Posts
    124
    I may very well be wrong about this, but I don't think the address-of operator can be overloaded. I know that the pointer operator (*) can't be, so I assume the (&) operator can't be overloaded either.

  3. #3
    Registered User
    Join Date
    Dec 2002
    Posts
    103
    Hi Maxhavoc,

    I know that it can be overloaded and I know it works.
    Only few operators cannot be overloaded (. , .*, ::, ?:, sizeof) and the address-of operator is not one among them. The above example what I have written is "RUBBISH" w.r.t its Usefulness. It could be used in the context of SmartPointers.
    But I just can't see as to how its working.

    Shiv
    Have a wonderful day.... and keep smiling... you look terrific that way
    signing off...
    shiv... as i know him

  4. #4
    Registered User
    Join Date
    Jun 2004
    Posts
    124
    Well, if it does work, then shouldn't it just return the address of the class object? It won't be a recursive call because it's not pointing to a function, it's pointing to the class object. Though this is just conjecture on my part.

  5. #5
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,510
    Do not confuse operator&() with operator&(T const &)

    The first is the address-of operator and the second is the bitwise AND operator.

    What is passed to the address-of operator is the implicit This.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  6. #6
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    He's not confusing the two. I think the point is that the 'this' pointer is not resolved by calling the functions own address-of operator. I think basically, the question is how is the 'this' resolved, because it must not be the classes own address-of operator or we would experience recursion. Perhaps it uses the global operator & or keeps track of this seperately. Too bad I can not find any proof

  7. #7
    Registered User
    Join Date
    Dec 2002
    Posts
    103
    Thanks for understanding my question correctly Tonto.
    I just can't sleep in peace without understanding how this is working.

    Shiv

  8. #8
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,510
    It still illudes me... the question, that is

    But I think the answer relies on the fact that the this pointer is not something that exists with the object. It is only created when a member function is called. As such, before the overloaded operator processes its body, the This pointer was already created by simply giving it the address of the calling object.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  9. #9
    Registered User
    Join Date
    Dec 2002
    Posts
    103
    How can it take the address without the use of & operator on itself???

    Shiv

  10. #10
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,510
    That I don't know

    What I do know because it is what I researched just then on Stroustrup's 3rd Edition is that the This pointer is created when the member function is called and before the function body is processed (just like a regular parameter it was).

    As Tonto suggested... maybe some global address-of operator. Someone will know better...
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  11. #11
    Registered User
    Join Date
    Dec 2002
    Posts
    103
    Hmmmm I would like to exhaust all options before disturbing THE MAN.... Stroustrup ofcourse...
    I am also referring to his book apart from Thinking in C++ etc and could not find any clear info, that would bring me out of this darkness. Actually its "Thinking in C++" that got me into this situation in the first place

    Shiv
    Have a wonderful day.... and keep smiling... you look terrific that way
    signing off...
    shiv... as i know him

  12. #12
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    I don't think any operators are called if you return the this pointer. Shouldn't every object have an address? What about this:
    Code:
    #include <iostream>
    
    class Foo
    {
    public:
      Foo(int d):data(d){}
      void Bar()
      {
        Foo* temp = &(*this); //output: "& operator"
        temp = this; //no output
        std::cout<<temp->data<<std::endl;
      }
      Foo* operator&()
      {
        std::cout<<"& operator"<<std::endl;
        return this;
      }
    
    private:
      int data;
    };
    
    int main()
    {
    
      Foo test(5);
      test.Bar();
     
    }
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  13. #13
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,510
    I'm going to risk an answer...

    One thing we know, the This pointer is not your average variable. You cannot define it or assign to it. This, as far as I can tell, means the This pointer is defined by the compiler according to the programming language rules. As such, it is not affected by your class definition. If you prefer it, the This pointer is defined by some implicit & operator.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  14. #14
    Registered User
    Join Date
    Dec 2002
    Posts
    103
    You don't get any output when you do temp = this because you are assigning a pointer's value into another. Only when an explicit use of & is done w.r.t an object, its operator&() gets called.

    shiv
    Have a wonderful day.... and keep smiling... you look terrific that way
    signing off...
    shiv... as i know him

  15. #15
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,738
    Code:
         X* operator&()    // Compiler internally should convert it as X* operator&(X* const this)
         {
              cout << " Why does it not result in a recursive call??? " << endl;
              return this;
         }
    This works because it returns a this pointer, and in your code an assignment actually happens: you don't get the address per-se. It's a broken operator.

    Just about the only place I could think of where you would think about using an overloaded address-of would be an implementation of smart pointers, but there are reasons not to do it there as well. One is that generic code assumes that applying address-of to X would return *X, but in smart pointers there is an additional level of indirection, so that will cause problems and potentially break things.

    Letting users of your class access to the address of any object makes handling memory difficult, so relying on the compiler generated default is okay in this case. Overloading & is confusing... @.@

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Overloading -> operator
    By Hunter2 in forum C++ Programming
    Replies: 0
    Last Post: 05-10-2004, 03:08 PM
  2. operator overloading
    By blue_gene in forum C++ Programming
    Replies: 6
    Last Post: 04-29-2004, 04:06 PM
  3. operator overloading
    By dP munky in forum C++ Programming
    Replies: 14
    Last Post: 04-18-2003, 11:45 AM
  4. Operator overloading MSVC++
    By AeroHammer in forum C++ Programming
    Replies: 4
    Last Post: 04-15-2003, 01:40 AM
  5. Full operator overloading in BCB5
    By Mario in forum C++ Programming
    Replies: 5
    Last Post: 06-01-2002, 02:15 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21