How do you overload the -> operator in C++?

Started by
7 comments, last by bob_the_third 22 years, 1 month ago
I''m working on a class used for garbage collection in C++. The class (GarbageObjectRef) is supposed to be used in place of a pointer to an object:
  
class Spaceship
{
  public:
    SpaceShip() {}
    int X, Y;
    void Draw();
};

void MyFunction()
{
  GarbageObjectRef<Spaceship>  pNewShip = new Spaceship;

  pNewShip->X = 0;    
  pNewShip->Y = 0;
  pNewShip->Draw();
}
  
I want to be able to treat the class as a pointer to a structure. Anybody know how to do this? (By the way, the above code is purely fictional and was meant to demostrate how I intend to use the indirection operator.)
Advertisement
AFAIK, it isn't possible. You'll need to return the controlled pointer and then call operator -> on that. See CComPtr and CComQIPtr (both found in <atlbase.h> ) for examples.

[Edit: Damn smileys!]

[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! ]
Thanks to Kylotan for the idea!

Edited by - Oluseyi on February 25, 2002 8:30:39 PM
The "default" operator->() does this :
T* T::operator->() { return this; }

And interestingly, for those who care, if the return type is not a true pointer, the compiler will generate calls to operator->() until the return type is a true pointer.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
I thought it was impossible too, but I came across an article at
"http://www.codeproject.com/cpp/garbage_collect.asp?print=true" that seemed to indicate that it was possible to do; I read a source (another article on the net) that gave the list of unoverloadable (it''s a word now!) operators as "." ".*" "?"
Does anyone know where I can find the "official list" and perhaps a detailed refrence on the subject?
quote:The "default" operator->() does this :
T* T::operator->() { return this; }

And interestingly, for those who care, if the return type is not a true pointer, the compiler will generate calls to operator->() until the return type is a true pointer.

Apparently this was posted as I was typing my response to Oluseyi!

So if I want to overload the -> operator to make a class act like a pointer, would I do the following?
  <template TYPE>class GarbageObjectRef{  public:     ...     TYPE* operator->()     {         return (TYPE*)(m_pObject);     }     ...  private:     TYPE* m_pObject;   /*This is the pointer that saves the address of the newly created object that we are referencing.*/};void MyFunc(){  GarbageObjectRef<someClass>  pMyObject = new someClass;    pMyObject->value = 42;     /*Assuming these are members of someClass...*/  pMyObject->DoSomething();}  


Will this work?

Yes, that will work, my smart pointer class does something just like that.

~CGameProgrammer( );

~CGameProgrammer( );Developer Image Exchange -- New Features: Upload screenshots of your games (size is unlimited) and upload the game itself (up to 10MB). Free. No registration needed.
Great! Thanks for the info. Maybe it''s time for me to buy a new C++ book...
Cool beans! Die, CComPtr, die!

[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! ]
Thanks to Kylotan for the idea!
If you wish a class to behave like a smart pointer, then you need to simulate the semantics of a dumb pointer, while adding some behaviour that defines it as "smart". The two significant things you can do with a pointer are:

- access a member through it''s T* operator->()
- dereference it through it''s T operator*()

Here''s an example of a smart pointer, with the proviso that it''s rather naive, and is only provided to demonstrate the required operator overloading...

  #include <iostream>template<typename T>class SMP{public:  explicit SMP( T* t ) : p_thing(t) {}  SMP() : p_thing(0) {}  void reset( T* p_t )  {    T* p_temp = p_thing;    p_thing = p_t;    delete p_temp;  }  ~SMP() { delete p_thing; }  T* operator->() const { return p_thing; }  T operator*() const { return *p_thing; }private:  T* p_thing;};struct A{  void print() const { std::cout << "A::print()" << std::endl; }};int main(){  SMP<A> smp(new A);  smp->print();  (*smp).print();}  


If you''re wondering why this smart pointer is naive, it''s because the ownership policy is not defined, which is not really very "smart".

Smart pointers are an excellent way of part automating your memory management. I recommend you investigate std::auto_ptr and the Boost libraries (www.boost.org) to see what is available before you reinvent the wheel.

--

It is against the law to stare at the mayor of Paris.

This topic is closed to new replies.

Advertisement