Topic: [++] Assignment operator overloading.


Author: Colin Rafferty <craffert@spssunp.spspme.ml.com>
Date: 1996/12/05
Raw View
In article <cbreece-0412960654310001@platinum.telogy.com>,
cbreece@telogy.com writes:

> What is wrong with the following, or why is it considered a ``bad
> thing''.  This seems to be more consistent with the rest of the operator
> rules.
>
> class A {
>   public:
>      void operator=(int &value, A &instance) {
>            value = instance.data;
>      }
>
>      operator=(char* &ptr, A &instance) {
>            ptr = instance.dataPtr;
>      }
>
>   private:
>      int data;
>      char *dataPtr;
> }

I am not certain what effect you desire, but I think that what you want
to be able to do is have code that looks something like this:

   void f(const A& instance)
   {
      int value = instance;
      char* ptr = instance;
      // ...
   }

Given this, all that you need are conversion operators.  Your new
member functions would look like this:

   A::operator int() { return data; }
   A::operator char*() { return dataPtr; }

If the prior function is not what you wanted, then I don't see what
effect you are trying to get.  Give me a code example that would use it.

--
This posting adheres to the SELF-DISCIPLINE guidelines for better
USENET discussions. See http://www.eiffel.com/discipline.
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]