473,406 Members | 2,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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?
Jul 22 '05 #1
6 1074

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

"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

23
by: Paul Rubin | last post by:
OK, I want to scan a file for lines matching a certain regexp. I'd like to use an assignment expression, like for line in file: if (g := re.match(pat, line)): croggle(g.group(1)) Since...
166
by: Graham | last post by:
This has to do with class variables and instances variables. Given the following: <code> class _class: var = 0 #rest of the class
47
by: fb | last post by:
Hi Everyone. Thanks for the help with the qudratic equation problem...I didn't think about actually doing the math...whoops. Anyway... I'm having some trouble getting the following program to...
7
by: Andy Lomax | last post by:
The C99 standard contains various statements like this one (in this case, 6.5.16, assignment operator): >If an attempt is made to modify >the result of an assignment operator or to access it...
7
by: Peter Bromley | last post by:
I have been reading about the proposed changes for C++/CLI and they look great. There is one thing that currently does not work well that I would like clarified. A managed C++ property can be...
19
by: scroopy | last post by:
Is it impossible in C++ to create an assignment operator for classes with const data? I want to do something like this class MyClass { const int m_iValue; public: MyClass(int...
34
by: Chris | last post by:
Is there ever a reason to declare this as if(*this == rhs) as opposed to what I normally see if(this == &rhs) ?
5
by: subramanian | last post by:
Consdier the following program: #include <stdio.h> struct typeRecord { int id; char str; } records = { {0, "zero"}, {1, "one"},
61
by: warint | last post by:
My lecturer gave us an assignment. He has a very "mature" way of teaching in that he doesn't care whether people show up, whether they do the assignments, or whether they copy other people's work....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.