Click to See Complete Forum and Search --> : operator= with base type as lhs
Talix
October 22nd, 2002, 02:12 PM
Ok, this example is in Stroustrup C++ 3rd ed.:
complex operator+ (double a, complex b)
{
...
}
But when I have this code:
float& operator= (float& lhs, const EnumType& rhs)
{
lhs = rhs.getInt();
return lhs;
}
I get this error:
'operator =' must be a <Unknown> member
Any ideas why this is happening?
-Talix
P.S. For those of you looking at my other question, I assumed that using the base class EnumType was ok for this one, even though operator= functions aren't inherited, because this is not a member function, and so should work for all subclass types, right? :)
PaulWendt
October 22nd, 2002, 02:21 PM
I don't think that operator= has the flexibility to be a non member.
You'll have to do something like:
class A
{
// ...
A& operator=(const A& rhs);
// ...
};
class B : public A
{
// ...
B& operator=(const B& rhs);
B& operator=(const A& rhs);
// ...
};
This is
--Paul
galathaea
October 22nd, 2002, 02:23 PM
You can expose the operator= as a friend function in your class.
Talix
October 22nd, 2002, 02:29 PM
Originally posted by PaulWendt
I don't think that operator= has the flexibility to be a non member.
That would be frustrating, since I want my class to act like a string or an integer, and being assigned to those without the use of cumbersome member functions is a relatively important piece...
Originally posted by galathaea
You can expose the operator= as a friend function in your class.
I don't believe that would be useful, since I cannot declare the operator= function in my class since the left hand side needs to be a base type. I already have floats successfully being assigned to my class, now I want the reverse functionality.
Anyone else think there might be a way to do this? :confused:
-Talix
PaulWendt
October 22nd, 2002, 03:08 PM
operator= has to be a member of a class; it cannot be globally
overloaded the way you want. You CAN, however, implement
a cast function so that you can cast your user-defined datatype
to another type. This would make the conversion implicit so that
you could do the assignment you're looking for.
Instead of "explaining" it with techno-babble, here's an
example:
class A
{
// ...
operator float() const { return getInt(); };
// ...
};
A a;
a.setInt(3);
float f = a; // f will be 3.0
Be careful with this approach. You can sometimes find your class
behaving like a float when you don't WANT it to. From what you
describe, it seems like your class is going to be some variant-type
... or that it HAS to behave like other types. For this example,
you'll probably be safe ... but be careful :)
--Paul
Talix
October 22nd, 2002, 03:16 PM
PERFECT! :D
Thank you so much, that worked great. It was exactly what I was looking for.
This forum rules. :cool:
Graham
October 23rd, 2002, 04:51 AM
But beware the dangers of introducing user-defined conversions into your class. See More Effective C++ Item 5 - Be wary of user-defined conversion functions.
Personally, I'd go for a more explicit approach:
class A
{
public:
double as_double() {/* whatever */}
};
int main()
{
A a;
//......
double d = a.as_double();
}
More typing, fewer problems.
PaulWendt
October 23rd, 2002, 06:12 AM
Originally posted by Graham
But beware the dangers of introducing user-defined conversions into your class. See More Effective C++ Item 5 - Be wary of user-defined conversion functions.
Personally, I'd go for a more explicit approach:
I never thought I'd be saying this a couple of years ago, but I
wholeheartedly agree. For some reason, the original poster
didn't want any "cumbersome" function calls. Maybe he has weird
design requirements or something.... Either way, I'd agree with
Graham in that I think you should use the explicit conversion
functions if possible :)
--Paul
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.