Hi,
Im trying to overload the assignment operator and would like to clear a few things up if thats ok.
I have a non member function, bool operator==( const MyClass& obj1, const myClass& obj2 )
defined oustide of my class.
I cant get at any of my private members for obvious reasons.
So what I think I need to do is to overload the assignment operator. And make assignments in the non member function.
With that said, I think I need to do the following:
- use my functions and copy information using strcpy or strdup. I used strcpy.
- go to the assignment operator, bool MyClass::operator=( const MyClass& obj1 );
- Now we go to the function overloading (==) and assign obj2 to obj1.
I dont have a copy constructor, so I'm stuck with these:
class Class
{
private:
m_1;
m_2;
public:
..
};
void Class::Func1(char buff[]) const
{
strcpy( buff, m_1 );
return;
}
void Class::Func2(char buff[]) const
{
strcpy( buff, m_2 );
return;
}
bool Class& Class::operator=(const Class& obj)
{
if ( this != &obj ) // check for self assignment.
{
strcpy( m_1, obj.m_1 );
// do this for all other private members.
}
return *this;
}
bool operator== (const Class& obj1, const Class& obj2)
{
Class MyClass1, MyClass2;
MyClass1 = obj1;
MyClass2 = obj2;
MyClass2 = MyClass1;
// did this change anything?
// Microsofts debugger can not get this far.
return true;
}
So as you can probably tell, I'm completely lost in this overloading. Any tips? I do have a completed version overloading the same operator, only with ::
, so my private members won't lose scope. I return my assignments as true and it works in main
. Which is the example that I have in my book.
Will overloading the assignment operator and then preforming conversions in the operator==
non member function work? Will I then be able to assign objects to each other in main after having completed that step?