Jump to content
  • Advertisement
Sign in to follow this  
MrPoopypants

"Pointers" in java

This topic is 4810 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, I'm writing a class in Java called VERTEX4D and I want it to be as similiar to a c-style struct as possible. I want the data for the vertex to be held in two ways: the first as x , y , z , w coordinates and the other in an array with four elements. However, I want to do this as simply as possible, and dont want to have to write several methods constantly setting the data equal to each other. I want x, for example, to be: COORDS4D [ 0 ] = &x , using C-style pointers, so when accessing this class both *COORDS4D [ 0 ] and x always equal the same thing. Obviously, the * and & operators do not exist in java, so I'm at a loss of what to do. Any help would be great, thanks.

Share this post


Link to post
Share on other sites
Advertisement
You can't overload operators like [] in Java. The language philosophy is that you are not to be trusted with such things.

Share this post


Link to post
Share on other sites
I think you can't do operator overloading at all in Java. There are some built-in things that use it, but IIRC you can't do it yourself.

Share this post


Link to post
Share on other sites
Ok using your example.. You could do this a number of ways

//lets initialize first
ArrayList coords4DList = new ArrayList() // I typically use array list

or

VERTEX4D[] coordArray = new VERTEX4D[NUM_OF_COORDS]; // if you prefer an array


//so now we create your coord object from the class

VERTEX4D x = new VERTEXT4D();

// now we will add it to both the array list and the array

coords4DList.add(x);
coordArray[0] = x;

// at this point

(VERTEX4D) coords4DList.get(0) == x; // these point to the same object in memory
coordArray[0] == x; //these point to the same object in memory
(VERTEX4D) coords4DList.get(0) == coordArray[0]; // so obviously these point to the same object as well

When you use the '=' operator on an object you are passing by value a reference. They arn't the same as C++ references rather more like C++ pointers that are always dereferenced when you access them.

Share this post


Link to post
Share on other sites
Oh, I forgot to mention. If you want the array to hold a reference to a different object in memory but same values as x at copy time, you will need to clone the object. You can have the class implement clone and then pass a clone instead.

Share this post


Link to post
Share on other sites
vertex.a = 0
vertex.b = 2
vertex....

You get the idea

You are somewhat confuzed in capabilities of programming language and a usage of an other language. Just because in one language people (wrongly) abuses pointers (in a way that might be VERY bad on multiple CPU computers, or for compiler optimalizations.) it doesn't mean you should mindlessly copy all that syntax, and ignore the real algorithm.
You could easily access an public member of an object in Java. You should note that sometimes it could be faster to acces it throught get and set method and have it private. It depends on traps and JIT compiler optimalizations.

BTW I hope you don't like to do some aliasing. Dazzling of JIT compiler isn't recommended. Dazzling of users of a possible library isn't recommended either. Of course you could use ByteBuffer... at a highly likely speed penalty. (Actually not as high if you'd set native order, but you could forget inlining.)

Re ArrayList
I would very not recomend a class that have variable size as a base of class that has finite amount of members.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!