By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,192 Members | 867 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,192 IT Pros & Developers. It's quick & easy.

assignment behaviour problems

P: n/a
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?
Jul 22 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a

"velthuijsen" <ve*********@hotmail.com> wrote in message
news:e5*************************@posting.google.co 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
Jul 22 '05 #2

P: n/a
On Wed, 19 May 2004 19:35:32 +0530, "Sharad Kala"
<no*****************@yahoo.com> wrote:

"velthuijsen" <ve*********@hotmail.com> wrote in message
news:e5*************************@posting.google.c 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::operator=()" << endl;
return *this; }
MyClass& operator=(int Right)
{ cout << "MyClass::operator=(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::operator=()
MyClass::operator=(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
Jul 22 '05 #3

P: n/a
On 19 May 2004 07:00:32 -0700, ve*********@hotmail.com (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
Jul 22 '05 #4

P: n/a

"Leor Zolman" <le**@bdsoft.com> wrote in message
news:7e********************************@4ax.com...
On Wed, 19 May 2004 19:35:32 +0530, "Sharad Kala"
<no*****************@yahoo.com> 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
Jul 22 '05 #5

P: n/a
On Wed, 19 May 2004 10:44:08 -0400, Leor Zolman <le**@bdsoft.com> 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
Jul 22 '05 #6

P: n/a
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.
Jul 22 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.