0

Consider this Java code snippet:

Vertex a = graph.addVertex(null);
Vertex b = graph.addVertex(null);

Is it possible to do the same in C++? AFAIK only option in C++ would be:

Vertex& a = graph.addVertex(NULL);

Or maybe this:

typedef Vertex& Vertexref;
Vertexref a = graph.addVertex(NULL);

Of course returning a copy of the object is also possible.

But Java syntax seems more straightforward to me. Would it be possible to have it in C++?

4
  • 1
    You can modify the Vertex class to store shared pointers to its data, if you don't really care about performance. Commented Jan 1, 2014 at 18:14
  • 1
    Don't typedef a reference, or the people who have to maintain your code will hunt you down. Commented Jan 1, 2014 at 18:22
  • @Benjamin shared_ptr performance is pretty good, and usually good enough - especially since the compiler will elide many of the copies. Commented Jan 1, 2014 at 18:26
  • I do not intend to typedef a reference. I just wanted to make clear it is possible in order to have a syntax more similar to Java. I am aware of the drawbacks of doing so. Commented Jan 1, 2014 at 18:27

2 Answers 2

5

You may think the Java syntax is more straightforward, but have you tried returning a copy of an object in Java? That's far less straightforward. C++ makes you opt in to references, whereas Java makes you opt in to not references. Swings and roundabouts.

You have already shown us the two ways to write this in C++, and they are both very straightforward.

Sign up to request clarification or add additional context in comments.

4 Comments

It also seems wrong to me. Sometimes I wonder why C++ doesn't provide a mechanism to return references without needing the caller to explicitly create a reference.
@jbgs: Are you saying, you wonder why C++ doesn't provide a mechanism for a variable that looks like a value of type Vertex, but if it was initialized using a call to addVertex is actually a reference to type Vertex? Aside from implementation difficulty, the reason is that this would be incredibly difficult to read. In C++ Vertex a; always declares a value (if Vertex is a class type). In Java Vertex a; always declares a reference. Why would you want a feature in either language to make it difficult to tell which you have? Explicitly stating the difference is good.
Why not? When you call a function there's no way to know if you are passing by reference or by value (unless you have a look at function's signature). Cannot we have the same issue for return values? I agree it might be inconvenient anyway.
Sure, let's take an inconvenience, and apply it somewhere else too. That's fun.
5

Java objects are referenced by pointers, even if it's not visible in the syntax in the same way as in C++. Your Java function doesn't really return a reference (in the C++ sense, not just a synonym for pointer), but it returns a pointer, and that pointer is returned by value. So the equivalent C++ would be something like this:

Vertex* a = graph.addVertex(null);
Vertex* b = graph.addVertex(null);

It is a mistake to think of the "references" in Java as the same thing as the "references" in C++. The Java "references" are pointers. C++ "references" are aliases for variables, which is not the same thing as a pointer, even though they are usually (but not always) implemented "under the hood" using pointers.

To clarify: This is not just terminology nitpicking. Your C++ program will not behave the same as your Java program, if you just translate Java references to C++ references. To make an equivalent C++ program, you need to use pointers.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.