This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Overloading reference operator


On 04/24/2010 06:30 PM, Samkit Jain wrote:
> template<class T>
> MyClass
> {
>  public:
>     MyClass() : m_data(10) { }
>     operator T&() { return m_data; }
>  private:
>     T m_data;
> };

I think this example does what you want:

template<class T> class
MyClass
{
public:
  MyClass() : m_data(10) { }
  operator T&() { return m_data; }
  T& operator= (const T &t) { m_data = t; return *this; }
private:
  T m_data;
};

Can you see why the definition of operator= does the trick?

Andrew.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]