Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > assignment behaviour problems

Reply
Thread Tools

assignment behaviour problems

 
 
velthuijsen
Guest
Posts: n/a
 
      05-19-2004
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?
 
Reply With Quote
 
 
 
 
Sharad Kala
Guest
Posts: n/a
 
      05-19-2004

"velthuijsen" <> wrote in message
news: m...
> 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


 
Reply With Quote
 
 
 
 
Leor Zolman
Guest
Posts: n/a
 
      05-19-2004
On Wed, 19 May 2004 19:35:32 +0530, "Sharad Kala"
<> wrote:

>
>"velthuijsen" <> wrote in message
>news: om...
>> 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:perator=()" << endl;
return *this; }
MyClass& operator=(int Right)
{ cout << "MyClass: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:perator=()
MyClass: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
 
Reply With Quote
 
Leor Zolman
Guest
Posts: n/a
 
      05-19-2004
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
 
Reply With Quote
 
Sharad Kala
Guest
Posts: n/a
 
      05-19-2004

"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


 
Reply With Quote
 
Leor Zolman
Guest
Posts: n/a
 
      05-19-2004
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
 
Reply With Quote
 
velthuijsen
Guest
Posts: n/a
 
      05-21-2004
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
debugger behaviour different to execution behaviour Andy Chambers Java 1 05-14-2007 09:51 AM
Assignment operator self-assignment check Chris C++ 34 09-26-2006 04:26 AM
Augument assignment versus regular assignment nagy Python 36 07-20-2006 07:24 PM
What would it take to change the behaviour of variable assignment? Daniel Nugent Ruby 10 10-30-2005 02:13 AM
Undefined behaviour when modifying the result of an assignment operator Andy Lomax C Programming 7 06-24-2005 09:27 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57