|
-
October 22nd, 2002, 02:12 PM
#1
operator= with base type as lhs
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?
-
October 22nd, 2002, 02:21 PM
#2
I don't think that operator= has the flexibility to be a non member.
You'll have to do something like:
Code:
class A
{
// ...
A& operator=(const A& rhs);
// ...
};
class B : public A
{
// ...
B& operator=(const B& rhs);
B& operator=(const A& rhs);
// ...
};
This is
--Paul
-
October 22nd, 2002, 02:23 PM
#3
also
You can expose the operator= as a friend function in your class.
-
October 22nd, 2002, 02:29 PM
#4
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?
-Talix
-
October 22nd, 2002, 03:08 PM
#5
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:
Code:
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
-
October 22nd, 2002, 03:16 PM
#6
PERFECT!
Thank you so much, that worked great. It was exactly what I was looking for.
This forum rules.
-
October 23rd, 2002, 04:51 AM
#7
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:
Code:
class A
{
public:
double as_double() {/* whatever */}
};
int main()
{
A a;
//......
double d = a.as_double();
}
More typing, fewer problems.
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
-
October 23rd, 2002, 06:12 AM
#8
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|