0

I have some code in C++

struct Node {
    int x;

    Node(int x) : x(x) {}

    friend std::ostream & operator << (std::ostream &o, const Node &n) {
        o << n.x;
        return o;
    }
};

void Foo(Node n) {
    n.x--;
    cout << "Foo: " << n << endl;
}

void Foo2(Node &n) {
    n.x--;
    cout << "Foo2: " << n << endl;
}

int main() {
    Node n(5);
    cout << n << endl;
    Foo(n);
    cout << n << endl;
    Foo2(n);
    cout << n << endl;

    return 0;
}

...and the output is

5
Foo: 4
4
Foo2: 3
4

...now I reached the same in Java

class Node {
    public int x;

    Node(int x) {
        this.x = x;
    }

    @Override
    public String toString() {
        return Integer.toString(x);
    }
}

public class NodeHomevork {
    static void Foo(Node n) {
        n.x--;
        System.out.println("Foo: " + n);
    }

    static void Foo2(Node n) {
        Node n2 = new Node(n.x);
        n2.x--;
        System.out.println("Foo2: " + n2);
    }

    public static void main(String[] args) {
        Node n = new Node(5);
        System.out.println(n);
        Foo(n);
        System.out.println(n);
        Foo2(n);
        System.out.println(n);
    }
}

But I don't know how to implement Foo2 without using new keyword inside Foo2. If you have any ideas I'll be grateful for any posts, thanks.

  • `Java` is a reference type language. there is no way to avoid `new` when you want to create a variable. `C#` has `struct`s that they are value type. It mean you can create a variable of `struct`s without using new. On the other hand `C++` is completely a value type language and one of the reasons that `C++` performance is superior is duo to value types. – MRB Jan 21 '17 at 17:20
  • Possible [dupe](http://stackoverflow.com/questions/869033/how-do-i-copy-an-object-in-java) ? If not, highly related. The only thing to add is that only pass by value of reference exists in java. – George Jan 21 '17 at 17:23
  • You can just move the `new` to somewhere else. The cleanest solution would be to add a static method like `Node newInstanceFromExisting(Node node)` to your class, so that `Foo2` would just say `Node n2 = Node.newInstanceFromExisting(n);`. The `new` would then be inside `newInstanceFromExisting`. – Christian Hackl Jan 21 '17 at 17:28
  • 2
    Java is not C++, C++ is not Java. Don't try and write C++ code using Java as a model, and don't try to write Java code using C++ as a model. – PaulMcKenzie Jan 21 '17 at 17:32
  • @PaulMcKenzie - you shouldn't to teach me what I would to do. My teacher require this to learn C++ and Java differences. – Владимир Jan 21 '17 at 17:50
  • @Christian Hackl - thanks for your idea, its also a good way what I think my teacher require. – Владимир Jan 21 '17 at 17:51
  • 1
    @Владимир I have no idea what a "teacher" is telling you to do. What I'm saying is that Java and C++ are two different languages, and trying to convert from one to the other by attempting to do line-by-line translations doesn't work. – PaulMcKenzie Jan 21 '17 at 17:53

1 Answers1

0

In C++ when you put an argument to Foo2 your object is copied. In Java you can use clone() method:

class Node implements Cloneable
{
    public int x;

    Node(int x) { this.x = x; }

    @Override
    public String toString() { return Integer.toString(x); }

    @Override
    protected Node clone() throws CloneNotSupportedException
    {
        Node copy = null;
        copy = (Node)super.clone();
        return copy;
    }
}

Foo2 code:

static void Foo2(Node n)
{
    Node n2 = n.clone();
    n2.x--;
    System.out.println("Foo2: " + n2);
}

OR:

@Override
public Node clone() throws CloneNotSupportedException { ... } //public!

static void Foo2(Node n)
{
    n.x--;
    System.out.println("Foo2: " + n);
}

And call via Foo2(n.clone()); But I think you should use first option, because in C++ code your clone is inside method.