CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Oct 2002
    Location
    Walled Lake, MI, USA
    Posts
    8

    Question 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?

  2. #2
    Join Date
    May 2000
    Location
    Phoenix, AZ [USA]
    Posts
    1,347
    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

  3. #3
    Join Date
    Sep 2002
    Posts
    1,747

    also

    You can expose the operator= as a friend function in your class.

  4. #4
    Join Date
    Oct 2002
    Location
    Walled Lake, MI, USA
    Posts
    8
    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

  5. #5
    Join Date
    May 2000
    Location
    Phoenix, AZ [USA]
    Posts
    1,347
    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

  6. #6
    Join Date
    Oct 2002
    Location
    Walled Lake, MI, USA
    Posts
    8
    PERFECT!

    Thank you so much, that worked great. It was exactly what I was looking for.

    This forum rules.

  7. #7
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470
    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


  8. #8
    Join Date
    May 2000
    Location
    Phoenix, AZ [USA]
    Posts
    1,347
    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
  •  





Click Here to Expand Forum to Full Width

Featured