assignment behaviour problems

V

velthuijsen

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

Sharad Kala

velthuijsen said:
I've created a class
MyClass

Should be class 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
 
L

Leor Zolman

Should be class MyClass
Make this -
explicit MyClass(int);


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
 
L

Leor Zolman

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
 
S

Sharad Kala

Leor Zolman said:
On Wed, 19 May 2004 19:35:32 +0530, "Sharad Kala"
[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
 
V

velthuijsen

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top