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