Java references are much closer to C++ pointers rather than C++ references. In Java, you can do the following with a reference:
- Change which object it refers to
- Check whether two references are equal or unequal
- Send messages to the referenced object.
In C++, pointers have these same properties. As a result, the code you're looking for in C++ is something like
float* f = new float;
Which is perfectly legal. For a better comparison, this Java code:
String myString = new String("This is a string!"); // Normally I wouldn't allocate a string here, but just for the parallel structure we will. System.out.println(myString.length()); /* Reassign myString to point to a different string object. */ myString = new String("Here's another string!"); System.out.println(myString.length());
would map to this C++ code:
std::string* myString = new std::string("This is a string"); std::cout << myString->length() << std::endl; delete myString; // No GC in C++! /* Reassign myString to point to a different string object. */ myString = new std::string("Here's another string!"); std::cout << myString->length() << std::endl; delete myString; // No GC in C++!
Hope this helps!
swapfunction?)