views:

343

answers:

4

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:

  1. use my functions and copy information using strcpy or strdup. I used strcpy.
  2. go to the assignment operator, bool MyClass::operator=( const MyClass& obj1 );
  3. 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?

+6  A: 

Op== isn't the assignment operator. T& Op= (const T&) is.

bool operator==(const T& lhs, const T& rhs) is the operation to compare two Ts. It returns true if lhs is equal to rhs, for whatever definition of "equal" you want to code.

tpdi
+8  A: 

You have a couple of obvious mistakes here and there is some confusion about what you are actually trying to achieve. Firstly, the assignment operator operator = is meant to copy the value from one instance to another. The return value of the assignment operator is almost always a non constant reference to the target of the copy, so that you can chain assignments:

Class & operator=(const Class &rhs)
{
  // copy the members

  return *this;
}

The comparison operator operator == is meant to perform a comparison of two instances. It returns a boolean true if they are equal:

boolean operator==(const Class &rhs) const
{
  // check if they are equal
  return something;
}

The confusion is why are you trying to copy values around, or maybe assign to the instances in the comparison operator?

1800 INFORMATION
You have a little mistake, since assignment operator have right to left associative, we have that a = b = c is evaluated to (a = (b = c)), so it is ok, and recommended to return a const reference.
Ismael
yeah that's true, although effective C++ (Scott Meyers) says that the C++ standard says that you should return non-const reference. This is to make it compatible with the (admitedly weird) behaviour of builtins, where it is actually legal to do (a = b) = c for something like ints
1800 INFORMATION
A: 

I am not sure whether I understood the question correctly. But if you trying to check the equality using a non-member function and can't do this only because you can't access the private members of the class, then you can declare the non-member function as a friend function and use it like this:

class Test
{
public:
    Test(int i) : m_i(i){}
private:
    int m_i;

    friend bool operator==(Test&  first, Test& second);
};

bool operator==(Test&  first, Test& second)
{
    return first.m_i == second.m_i;
}

int main()
{
   Test t1(10), t2(10);
   bool b = (t1 == t2);
}
Naveen
Friend isn't a good option, I believe.
aJ
writing OP== in Class is just sufficient, right?
aJ
Friend is the way to go ... when we want to implement the comparison operator: unlike accessors (getters) that expose details, it enforces encapsulation (by hiding those ame details). But don't forget the "const"
Luc Hermitte
aJ, making op==() a member function will break the symetries in case the class support implicit conversions from other types.
Luc Hermitte
A: 

I am guessing that you want to compare the two objects. In that case, you can just overload the operator == in class "Class". You don't need assignment operator.

    class Class
    {
    public:
        Class(int i) : m_i(i){}

         bool operator==( const Class& rhs)
         {
         return m_i == rhs.m_i;
         }
    private:
        int m_i;

    };


int main()
{
   Class t1(10), t2(10);
   bool b = (t1 == t2);
}
aJ