Skip to main content
gamedev.net
🔒 Locked

java and pointers

Started by CProgrammer 5 replies 500+ views
Original Post
CProgrammer
CProgrammer
I know java doesnt support using pointers. Now my question is how should I do the following without pointers? In C++ i''d always have different classes handling different graphics drawing things. Each oth these classes has a pointer to a ''drawing class''. That way the ''drawing class'' was defined only once but could be used everywhere. How should I do this in java? I cant have a copy in each class, thats overkill. Do you guys suggest passing the value in with every call and retrieving it again after working with the class? Or what? -CProgrammer
johnnyBravo
johnnyBravo
lol its the complete opposite, java doesnt have non pointers.
George2
George2

Foo a = new Foo();
Foo b = a;


a doesn''t get copied here , b just points to the same instance of Foo which a points to. Java has pointers but they are hidden. So with your problem just new your Drawing class once, and in each object which needs to draw something store a ''pointer'' to it.



"THE INFORMATION CONTAINED IN THIS REPORT IS CLASSIFIED; DO NOT GO TO FOX NEWS TO READ OR OBTAIN A COPY." , the pentagon
CProgrammer
CProgrammer
Ah thanks guys. Seems my design still works. Although I like the Singleton approach. Thanks. I may use that instead.
-CProgrammer
Clu
Clu
quote:
Original post by johnnyBravo
lol its the complete opposite, java doesnt have non pointers.


Actually, it does. There are two types of objects (forgive my use of this very overloaded term): reference types and value types. All primitive types (int, boolean, float, byte, short, etc), are value types. This means that every time you pass an instance of a primite type around (through a function, for example), its value is copied. All other types, including all of the base Java library classes as well as all user-defined classes, are reference types. You allocate objects of these types with the "new" operator, and from that point on, you''re dealing with a reference to that object. The dot operator (.) performs a dereference; this is one place where Java and C++ differ. In C++, the dot operator is simply a member access operation, and you use the arrow operator (->) to actually do a pointer dereference.
etothex
etothex
quote:
Original post by Clu
quote:
Original post by johnnyBravo
lol its the complete opposite, java doesnt have non pointers.


Actually, it does. There are two types of objects (forgive my use of this very overloaded term): reference types and value types. All primitive types (int, boolean, float, byte, short, etc), are value types. This means that every time you pass an instance of a primite type around (through a function, for example), its value is copied. All other types, including all of the base Java library classes as well as all user-defined classes, are reference types. You allocate objects of these types with the "new" operator, and from that point on, you''re dealing with a reference to that object. The dot operator (.) performs a dereference; this is one place where Java and C++ differ. In C++, the dot operator is simply a member access operation, and you use the arrow operator (->) to actually do a pointer dereference.



Actually, they''re not reference types, they *are* pointers...

Just think of ''Object a'' as ''Object* a'' and ''.'' as ''->'' in c++, and you''ll be ok.

(they''re not reference types because otherwise you could do a stl -style swap() function in java)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.