assignment behaviour problems

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • velthuijsen

    assignment behaviour problems

    I've created a class
    MyClass
    {
    public:
    MyClass();
    MyClass(int);
    MyClass& operator=(const MyClass& Right);
    ...
    private:
    ...
    }

    In my code I used to test it out I have

    MyClass Test;
    ....
    int i = 5;
    ....
    Test = i;
    ....

    The Test = i results in a temporary MyClass being constructed using
    the MyClass(int) constructor.
    How can I prevent that from happening?
  • Sharad Kala

    #2
    Re: assignment behaviour problems


    "velthuijse n" <velthuijsen@ho tmail.com> wrote in message
    news:e54e13ae.0 405190600.bc264 04@posting.goog le.com...[color=blue]
    > I've created a class
    > MyClass[/color]

    Should be class MyClass[color=blue]
    > {[/color]

    [color=blue]
    > public:
    > MyClass();[/color]
    [color=blue]
    > MyClass(int);[/color]
    Make this -
    explicit MyClass(int);
    [color=blue]
    > MyClass& operator=(const MyClass& Right);
    > ...
    > private:
    > ...
    > }
    >
    > In my code I used to test it out I have
    >
    > MyClass Test;
    > ...
    > int i = 5;
    > ...
    > Test = i;
    > ...[/color]

    It should be fine now.

    -Sharad


    Comment

    • Leor Zolman

      #3
      Re: assignment behaviour problems

      On Wed, 19 May 2004 19:35:32 +0530, "Sharad Kala"
      <no.spam_sharad k_ind@yahoo.com > wrote:
      [color=blue]
      >
      >"velthuijsen " <velthuijsen@ho tmail.com> wrote in message
      >news:e54e13ae. 0405190600.bc26 404@posting.goo gle.com...[color=green]
      >> I've created a class
      >> MyClass[/color]
      >
      >Should be class MyClass[color=green]
      >> {[/color]
      >
      >[color=green]
      >> public:
      >> MyClass();[/color]
      >[color=green]
      >> MyClass(int);[/color]
      >Make this -
      >explicit MyClass(int);
      >[color=green]
      >> MyClass& operator=(const MyClass& Right);
      >> ...
      >> private:
      >> ...
      >> }
      >>
      >> In my code I used to test it out I have
      >>
      >> MyClass Test;
      >> ...
      >> int i = 5;
      >> ...
      >> Test = i;
      >> ...[/color]
      >
      >It should be fine now.[/color]

      Well, not quite. Now there won't be any match at all for an assignment with
      int as the right-hand operand. One option is to overload the assignment
      operator, providing a version that takes an int directly:

      #include <iostream>
      using namespace std;

      class MyClass
      {
      public:
      MyClass()
      { cout << "MyClass()" << endl; }
      explicit MyClass(int)
      { cout << "MyClass(in t)" << endl; }
      MyClass& operator=(const MyClass& Right)
      { cout << "MyClass::opera tor=()" << endl;
      return *this; }
      MyClass& operator=(int Right)
      { cout << "MyClass::opera tor=(int)" << endl;
      return *this; }
      // ...
      private:
      // ...
      };

      int main()
      {
      MyClass Test, Test2;

      Test = Test2; // uses operator=(const Test&)

      int i = 5;
      Test = i; // uses operator=(int)

      return 0;
      }

      Output:

      MyClass()
      MyClass()
      MyClass::operat or=()
      MyClass::operat or=(int)

      -leor

      --
      Leor Zolman --- BD Software --- www.bdsoft.com
      On-Site Training in C/C++, Java, Perl and Unix
      C++ users: download BD Software's free STL Error Message Decryptor at:
      An STL Error Decryptor for C++ by Leor Zolman of BD Software - available to download here

      Comment

      • Leor Zolman

        #4
        Re: assignment behaviour problems

        On 19 May 2004 07:00:32 -0700, velthuijsen@hot mail.com (velthuijsen) wrote:
        [color=blue]
        >I've created a class
        >MyClass
        >{
        >public:
        > MyClass();
        > MyClass(int);
        > MyClass& operator=(const MyClass& Right);
        > ...
        >private:
        > ...
        >}
        >
        >In my code I used to test it out I have
        >
        >MyClass Test;
        >...
        >int i = 5;
        >...
        >Test = i;
        >...
        >
        >The Test = i results in a temporary MyClass being constructed using
        >the MyClass(int) constructor.
        >How can I prevent that from happening?[/color]

        C++ will take the "path of least resistance", to a certain extent, in order
        to find a way to make things compile. Since you didn't provide an
        assignment operator that accepts an int directly, it figures out that it
        can get from what you've written to something that works with the
        assignment operator you /did/ provide: It applies the user-defined
        conversion of int-to-MyClass via MyClass(int). If you simply provide an
        additional assignment operator that takes int directly, it will use that
        right off the bat, and you'll be saved the extra constructor. See the code
        in my reply to Sharad in this thread.
        -leor


        --
        Leor Zolman --- BD Software --- www.bdsoft.com
        On-Site Training in C/C++, Java, Perl and Unix
        C++ users: download BD Software's free STL Error Message Decryptor at:
        An STL Error Decryptor for C++ by Leor Zolman of BD Software - available to download here

        Comment

        • Sharad Kala

          #5
          Re: assignment behaviour problems


          "Leor Zolman" <leor@bdsoft.co m> wrote in message
          news:7esma012gq 3gdabt70hb9965q m6cnq3pmm@4ax.c om...[color=blue]
          > On Wed, 19 May 2004 19:35:32 +0530, "Sharad Kala"
          > <no.spam_sharad k_ind@yahoo.com > wrote:
          >[/color]
          [snip][color=blue]
          > Well, not quite. Now there won't be any match at all for an assignment with
          > int as the right-hand operand. One option is to overload the assignment
          > operator, providing a version that takes an int directly:[/color]

          Right, I overlooked this aspect of the problem.

          Thanks,
          Sharad


          Comment

          • Leor Zolman

            #6
            Re: assignment behaviour problems

            On Wed, 19 May 2004 10:44:08 -0400, Leor Zolman <leor@bdsoft.co m> wrote:

            [color=blue]
            > Test = Test2; // uses operator=(const Test&)[/color]
            Sorry, that comment was supposed to say: const MyClass &
            -leor

            --
            Leor Zolman --- BD Software --- www.bdsoft.com
            On-Site Training in C/C++, Java, Perl and Unix
            C++ users: download BD Software's free STL Error Message Decryptor at:
            An STL Error Decryptor for C++ by Leor Zolman of BD Software - available to download here

            Comment

            • velthuijsen

              #7
              Re: assignment behaviour problems

              Thank you that was exactly what I was looking for.
              Normally I'd have build an operator=(const int) as Leor suggested but
              this time the assignment of the int must only happen at creation time
              and never elsewhere.

              Comment

              Working...