assignment behaviour problems

Discussion in 'C++' started by velthuijsen, May 19, 2004.

  1. velthuijsen

    velthuijsen Guest

    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?
    velthuijsen, May 19, 2004
    #1
    1. Advertising

  2. velthuijsen

    Sharad Kala Guest

    "velthuijsen" <> wrote in message
    news:...
    > I've created a class
    > MyClass


    Should be class MyClass
    > {



    > public:
    > MyClass();


    > MyClass(int);

    Make this -
    explicit 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;
    > ...


    It should be fine now.

    -Sharad
    Sharad Kala, May 19, 2004
    #2
    1. Advertising

  3. velthuijsen

    Leor Zolman Guest

    On Wed, 19 May 2004 19:35:32 +0530, "Sharad Kala"
    <> wrote:

    >
    >"velthuijsen" <> wrote in message
    >news:...
    >> I've created a class
    >> MyClass

    >
    >Should be class MyClass
    >> {

    >
    >
    >> public:
    >> MyClass();

    >
    >> MyClass(int);

    >Make this -
    >explicit 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;
    >> ...

    >
    >It should be fine now.


    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(int)" << endl; }
    MyClass& operator=(const MyClass& Right)
    { cout << "MyClass::eek:perator=()" << endl;
    return *this; }
    MyClass& operator=(int Right)
    { cout << "MyClass::eek:perator=(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::eek:perator=()
    MyClass::eek:perator=(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:
    www.bdsoft.com/tools/stlfilt.html
    Leor Zolman, May 19, 2004
    #3
  4. velthuijsen

    Leor Zolman Guest

    On 19 May 2004 07:00:32 -0700, (velthuijsen) wrote:

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


    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:
    www.bdsoft.com/tools/stlfilt.html
    Leor Zolman, May 19, 2004
    #4
  5. velthuijsen

    Sharad Kala Guest

    "Leor Zolman" <> wrote in message
    news:...
    > On Wed, 19 May 2004 19:35:32 +0530, "Sharad Kala"
    > <> wrote:
    >

    [snip]
    > 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:


    Right, I overlooked this aspect of the problem.

    Thanks,
    Sharad
    Sharad Kala, May 19, 2004
    #5
  6. velthuijsen

    Leor Zolman Guest

    On Wed, 19 May 2004 10:44:08 -0400, Leor Zolman <> wrote:


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

    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:
    www.bdsoft.com/tools/stlfilt.html
    Leor Zolman, May 19, 2004
    #6
  7. velthuijsen

    velthuijsen Guest

    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.
    velthuijsen, May 21, 2004
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. nagy
    Replies:
    36
    Views:
    1,007
    Terry Reedy
    Jul 20, 2006
  2. Andy Lomax
    Replies:
    7
    Views:
    304
    Andy Lomax
    Jun 24, 2005
  3. Chris
    Replies:
    34
    Views:
    1,515
  4. Andy Chambers
    Replies:
    1
    Views:
    384
    Daniel Dyer
    May 14, 2007
  5. Daniel Nugent
    Replies:
    10
    Views:
    183
    Phil Tomson
    Oct 30, 2005
Loading...

Share This Page