• Advertisement

Archived

This topic is now archived and is closed to further replies.

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

This topic is 5898 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.)

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

  • Advertisement