Assignment Operator Overloading Java

10,920

Solution 1

Operator overloading is Not supported by Java.

Operator overloading violates one of the central tenets of the Java language design : transparency. It is against the language philosophy trying to overload an operator and should be avoided ...

Solution 2

You're going to need to write an explicit copy constructor. In Java, everything's a pointer (or "reference" as they call them), so when you assign p to q, you're just saying that p and q point to the same object.

q = new Queue(p)

Is what you want, assuming you've implemented the appropriate constructor.

EDIT: What you're proposing is just not possible with the syntax "q = p". If you want to "simulate" the assignment operator, then you're going to need to write a new method in Queue. For example, here's how Java programmers "simulate" overloading the addition operator:

a.add(b);

So in your case you'll need to write an "assign" method that takes a Queue, copies its data, and assigns these copies to the internal data of the other object.

q.assign(p);

public void assign(Queue other) {
  this.primitive = other.primitive;
  this.someObject = new SomeObject(other.someObject);
}

That's about as close as you're going to get.

Solution 3

Operator overloading cannot be done in Java. It requires support from the compiler to do just that. You can however create a preprocessor that understands java syntax and run it before building, that converts the call of your operator to an appropriate function but that is just overkill.

clone is not not related to operator overloading.

Solution 4

There is no "equivalent" because you cannot overload the assignment operator in Java. You're attempting to imitate the behavior with the clone() method, but you've got it backwards. Without operator overloading, you have three options:

  1. Write a function that mutates the invoking object to become a copy of the one passed in.
  2. Write a function that makes a copy of the invoking object and returns it to something else that will store it.
  3. Use a copy constructor.

1. Write a function that mutates the invoking object to become a copy of the one passed in.

Note that this is identical to the assignment operator overload, except it's named "copy" instead of "operator=".

If you want to do this:

foo A;
foo B;
A.copy(B);  // A is now a copy of B.

In C++:

foo& copy(const foo& rhs)
{
    if(&rhs != this)
    {
        this->m_someInt = rhs.m_someInt;
        this->m_someBar = rhs.m_someBar;
    }
    return *this;
}

In Java:

foo copy(foo rhs)
{
    this.m_someInt = rhs.m_someInt;
    this.m_someBar.copy(rhs.m_someBar);
    return this;
}

Note that in C++, assigning this->m_someBar to rhs.m_someBarcopies rhs.m_someBar by value, but in Java such an assignment would cause both foo objects to share the same Bar object. Therefore, a similar copying mechanism must be used on your subojects as well if you don't intend for them to be shared.

2. Write a function that makes a copy of the invoking object and returns it to something else that will store it.

If you want to do this:

foo A;
foo B;
A = B.clone();  // A is now a copy of B.

In C++:

foo clone() const  // Ignoring the fact that this is unnecessary in C++ where assignments (and returns) are by-value anyway.
{
    foo theCopy;
    theCopy.m_someInt = this->m_someInt;
    theCopy.m_someBar = this->m_someBar;
    return theCopy;
}

In Java:

foo clone()
{
    foo theCopy = new foo();
    theCopy.m_someInt = this.m_someInt;
    theCopy.m_someBar = this.m_someBar.clone();
    return theCopy;
}

3. Use a copy constructor.

If you want to do this:

foo B;
foo A = new foo(B);  // Java version
foo A(B); // C++ version

In C++:

foo(const foo& rhs)
  :m_someInt(rhs.m_someInt), m_someBar(rhs.m_someBar)
{
}

// Or optionally:
foo(const foo& rhs) = default;

In Java:

foo(foo rhs)
{
    m_someInt = rhs.m_someInt;
    m_someBar = new Bar(rhs.m_someBar);
}

In conclusion, your issue is that you're trying to use clone() in the same way I use copy() above. The closest to an equivalent would be my copy method. My personal suggestion is to just use a copy constructor and call it a day.

Share:
10,920
Chris
Author by

Chris

Updated on November 23, 2022

Comments

  • Chris
    Chris over 1 year

    Im having trouble figuring out how to implement the equivalent of overloading the assignment operator in C++ to Java. I know there is no such thing, but I need to simulate it. I've tried overriding the Clone() function, but no luck. Any ideas?

    Below is my main

     Queue p = new Queue();
     Queue q = new Queue();
    
        p.enqueue('a');
        p.enqueue(9);
        p.enqueue(10);
        p.enqueue(310);
        p.enqueue(8);
    
        q = p;
        System.out.print(p);
    

    And here is the clone function

    public void Clone(Queue other) throws Throwable
    {
        System.out.println("test\n");
    
        if(this == other)
        {
    
        }
        else
        {            
    while(!isEmpty())
      dequeue();
    
    Node tmp = other.head;
    while(tmp != null){
        this.enqueue((T)tmp.element);
        tmp = tmp.next;
    
    }   
        }
    
    }
    
    • Aniket Inge
      Aniket Inge about 11 years
      How is clone() related to operator overloading?
    • Perception
      Perception about 11 years
      The thought that you want to overload the = operator is downright scary. Please browse through your very own code sample and think about why that would be a very, very bad thing.
    • derpface
      derpface almost 10 years
      @Aniket It copies an object. Copying objects is typically what operator overloading is done for. That's how they're related. See Cloning.
  • Chris
    Chris about 11 years
    OK, so how would you go about copying and object using "q = p" and having it perform a Deep Copy. Because when I run that code, it copies, but only a shallow copy. So both objects point to the same object.
  • Aniket Inge
    Aniket Inge about 11 years
    Yes that's how the assignment operator works for Java. You cannot override that functionality and make it do a deep copy. You might want to use a function to do that or a preprocessor like I answered.
  • Chris
    Chris about 11 years
    Yes i have implemented that constructor. And it works. But my task is to implement the equivalent to the Assignment operator in C++ to Java.
  • Chris
    Chris about 11 years
    Im not trying to overload the operator, im just trying to simulate it in Java.