Quote:
Originally Posted by HelloWorld Thanx a lot Bench Let me know if I'm wrong, so pointer is basically change the primitive type variable to be a reference type? Is that what's pointer does? (Since that's what a reference type variable a.k.a Object does too...  ) |
I have a feeling your understanding of an "object" is a little different, perhaps I should clarify from a C++ perspective:
An object is something stored in memory, with a certain type, eg, int, double, some class-type, etc (What distinguishes the object from raw 1's and 0's is the fact that the type tells the program what kind of data its dealing with, and how big the area of memory is, otherwise the data would be meaningless)
Following on from that, A variable is a named object. Neither objects nor variables have anything to do with references in C++.
Perhaps, java text books restrict the term 'object' to mean "an instance of a class" - Most probably because Java doesn't allow variables of a class. in C++ an 'object' is a general term, which can be an instance of a class, or of a built-in type (int, float, etc).
I think Java texts muddy the water a little by interchanging the terms "object" and "reference" - Something which it really ought not to. Here's why: (Apologies for using java in a C++ forum)
Code:
public class Mess {
int i;
public static void main(String args[])
{
Mess my_mess = new Mess(5);
}
public Mess(int ii)
{
i = ii;
}
} In the above java snippet,
my_mess is a reference (a java
smart pointer) to an object.
However, the object itself, the new Mess instance has no name, and isn't 'owned' by the
my_mess reference (in fact, its owned by the Java garbage collector) - the two things are seperate entities, but the reference can be used to access that object. However, the object is not dependent on the existance of the reference, nor is the reference dependent on the existance of the object.
in C++, you can do the same thing with pointers
Code:
class Mess
{
int i;
public:
Mess(int ii)
{
i = ii;
}
};
int main()
{
Mess* my_mess = new Mess(5);
} This creates a pointer called my_mess, and an un-owned object (C++ hasn't got a garbage collector) of type Mess.
Contrast this with something that Java won't let you do, which is, create a variable of a class
Code:
class Mess
{
int i;
public:
Mess(int ii)
{
i = ii;
}
};
int main()
{
Mess my_mess = Mess(5);
} In this case, my_mess is the named object (Variable), type Mess. The variable owns the object in memory, and is responsible for its creation/destruction.
Since the variable owns object in memory, both are totally dependent on one another - the variable cannot be used if the object is destroyed (This is usually ok, because the object lasts as long as the variable exists), and the object cannot be used when the variable is destroyed (Sometimes a problem which can happen if you let a pointer keep ahold of the address of the object after the variable dies)
Note: I should probably correct myself from my earlier post - I think I implied that a pointer needed to be a named object (a variable), although, actually a pointer can be an unnamed object too
Edit:
Quote:
Originally Posted by rpgfan3233 By the way, C++ has references too, though I'm not sure they are anything like Java's references: |
Not very much alike, aside from the basic semantics, they're quite different - Java references are like Boost/C++ smart pointers - they're modifiable, copyable, and reference-counted for a built-in garbage collector.