Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

pointer vs non pointer

Status
Not open for further replies.

eric91850

Programmer
Jul 29, 2007
12
US
to delcare an object with pointer -
MyCar * mycar = new MyCar;
mycar->Create();

========================
to delcare an object without pointer -
MyCar mycar = new MyCar;
mycar.Create();

========================
for pointer we use ->
for non-pointer we use . (dot)

========================
I think for Java equivalent the -> is same as Java's . (dot)

========================
should I always you the MyCar * mycar = new MyCar; to initalize a new
Object?

since using -> is same as using (dot) in Java.

========================
when are the time to use the (dot) but not -> (non-pointer) then?
 
Hi, I'm not familiar with Java, but I know C++ and there is a big difference in what you wrote.....
because:
if you declare Car *MyCar= new Car(), you're not declaring an object, but a pointer to the object, this means that every function that takes Car * as a Parameter can modify the values stored there.
Instead if you declare Car MyCar = new Car() this is an object and if you pass it to a function, will make a copy of it and you can modify the copy but not the original value.

Here is an examples
Class Car
{
public:
CString PalteNumber;
public:
Car();
};
Car::Car()
{
PlateNumber="";
}

void Main()
{
Car MyCar= new Car();
MyCar.PlateNumber="A"
ChangePlate(MyCar);
cout >> MyCar.PlateNumber // Value in plate number is still "A"
Car *MyCar1 = new Car();
MyCar->PlateNumber="A";
ChangePlate2(MyCar1);
cout >>MyCar.PlateNumber // Value in PlateNumber is B
}
void ChangePlate(Car CarPar)
{
CarPar.PlateNumer="B"
}
void ChangePlate2 (Car * CarPar)
{
CarPar->PlateNumber="B";
}


To help here is the Stack view Calling ChangePlate

CarPar.PlateNumber="B";
MyCar.PlateNumber="A":

when ChangePlate ends the only thing that remains in the stack is

MyCar.PlateNumber="A":


Stack view Calling ChangePlate2

Pointer to the below element
MyCar.PlateNumber="A"; changes to B

when ChangePlate2 ends in the stack remains

MyCar.PlateNumber="B";

Did you understand ? Alex
 
To add to what alexbo was saying: If you are passing a pointer to a function youre only passing 4bytes, as opposed to the size of the actual object. If you have a class that has many members, the size of the object could easily exceed 4bytes. With all this extra data being sent to the functions, it could slow down your processing time at run time.
 
The concept of pointers does not exist in Java. Java only has references.
 
I'm not familiar with Java, however c++ has references as well. A reference in an implicit pointer. I would imagine references in java work similarly to pointers and references in c++. Pointers just hang on to the memory address of an object as to manipulate the object without having to pass the actual object. I would check out the C++ primer book to get a better idea.
 
eric91850 said:
should I always you the MyCar * mycar = new MyCar; to initalize a new Object?

No. You should always prefer to not use new in C++. In most cases creating an object locally works well and it has many advantages to dynamically allocating. This is how you create an object without a pointer (your code was wrong above):
Code:
MyCar mycar;

There are situations where you need to dynamically allocate with new. Generally you should use smart pointers to ensure proper cleanup (C++ doesn't use garbage collection).

As far as passing objects around. It does not matter whether you allocate the object dynamically with new or locally like in my example. You can pass them to functions by value or by reference. Which you choose depends on what your function needs to do. Just understand that things are passed by value by default, so if you don't want a copy made you need to specifically use pass-by-reference.

Note that pass-by-reference can be implemented with pointers or references. References are generally preferred, although there are some instances where using a pointer has advantages.
 
Code:
MyCar mycar = new MyCar;
This shouldn't even compile! new returns a pointer. mycar is not a pointer, so you can't assign a pointer to an object.

xwb said:
The concept of pointers does not exist in Java. Java only has references.
Well I'd have to disagree with that. Java likes to call their objects 'references', but they're closer to pointers than references. Java has neither pointers nor references, but instead has some strange combination of the two.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top