CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Jul 2007
    Posts
    249

    Arrow Assignment Operator

    Hi,
    What is the advantage of conversion function over assignment operator overloading.

    and why there is no return type in the conversion function though it returns an object

  2. #2
    Join Date
    Jan 2009
    Posts
    1,689

    Re: Assignment Operator

    Conversion functions (usually called cast functions) are for a completely different purpose than the assignment operator. The assignment operator assigns an object to another object of the same type. The cast function converts it to another type. And cast functions certainly do have return types, and they do not have to return objects.
    Code:
    struct foo {
       foo & operator = (const foo & other){ bar = other.bar; }  //assignment
       operator float(void){ return (float)bar; }  //cast
    private:
       int bar;
    };
    
    
    foo a;
    foo b;
    a = b;  //assignment
    float f = (float)a; //cast

  3. #3
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: Assignment Operator

    Quote Originally Posted by Rajesh1978 View Post
    and why there is no return type in the conversion function though it returns an object
    Quote Originally Posted by ninja9578 View Post
    And cast functions certainly do have return types, and they do not have to return objects.
    Well lets try to not confuse the OP.

    A cast operator does not declare the return type in its signature, because it is already included in the name. It is just a syntax quirk:

    Code:
    operator char*()
    This will cast to char*. It WILL return an "object", which will be of the type of the cast (here, it will return a char*). Note that the term "object" is used in a broad sense here, and can be a class, a pointer, a reference, etc...

    ----
    It is actually usually considered bad design to provide cast operators, as it can lead to unexpected call. Explicit conversion is usually regarded as a superior alternative.
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  4. #4
    Join Date
    Jan 2009
    Posts
    1,689

    Re: Assignment Operator

    Correct me if I'm wrong, can't you declare the cast operator with "explicit" like you do with constructors to avoid those unexpected calls?

  5. #5
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: Assignment Operator

    Quote Originally Posted by ninja9578 View Post
    Correct me if I'm wrong, can't you declare the cast operator with "explicit" like you do with constructors to avoid those unexpected calls?
    Only with C++11. So (virtually) no one is using it yet.

    The stl uses it for streams, with explicit casts to bool.
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  6. #6
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: Assignment Operator

    Quote Originally Posted by ninja9578 View Post
    The assignment operator assigns an object to another object of the same type.
    That's not correct. An assignment operator can take a different type of RHS than the type of the LHS, i.e. the assignment operator can be overloaded. When both types are the same, we are actually talking about the copy assignment operator. However, since other overloads of the assignment operator are used so rarely, most people just talk about the assignment operator when they mean the copy assignment operator.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  7. #7
    Join Date
    Jan 2009
    Posts
    1,689

    Re: Assignment Operator

    I see, I assumed you could use it with different parameters that just a const reference to it's own type, I've just never seen it. This seems dangerous for the same reason cast operators are, possible unexpected calls to it.

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