Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Overloading -> and ->* operators

Overloading -> and ->* operators

Scheduled Pinned Locked Moved C / C++ / MFC
c++tutorialquestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Alton Williams
    wrote on last edited by
    #1

    I'm sure there's a C++ expert who can explain who to overload the indirection to a member and deferencing a pointer. class A{ int m_i; float *m_pf }; My questions are how to use operator -> to return m_i, ->* to return m_pf. What parameters to I use when create the method. Does it work when you create a pointer to an object of type classA?

    D 1 Reply Last reply
    0
    • A Alton Williams

      I'm sure there's a C++ expert who can explain who to overload the indirection to a member and deferencing a pointer. class A{ int m_i; float *m_pf }; My questions are how to use operator -> to return m_i, ->* to return m_pf. What parameters to I use when create the method. Does it work when you create a pointer to an object of type classA?

      D Offline
      D Offline
      Diddy
      wrote on last edited by
      #2

      Well techincally it would be like this: class A { public: int m_i; float *m_pf; int operator->() const { return m_i; } float& operator*() const { return *m_pf; } }; But - while the * operator makes sense, the -> makes none at all. Why would you want a -> operator to return a basic type? (ie not a pointer to a class) In fact, I belive if you compile the above you will get: warning C4284: return type for 'A::operator ->' is 'int' (ie; not a UDT or reference to a UDT. Will produce errors if applied using infix notation) return type for 'identifier::operator –>' is not a UDT or reference to a UDT. Will produce errors if applied using infix notation The operator–>( ) function must return either a pointer to a class or an object of or a reference to a class for which operator–>( ) is defined. The following example causes this warning: class C { public: int operator->(); // warning }; It is meaningless to apply the member access operator to fundamental data types that have no member access. This warning message will appear when this happens.

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups