Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

pointer vs non pointer

pointer vs non pointer

pointer vs non pointer

to delcare an object with pointer -
 MyCar * mycar = new MyCar;

to delcare an object without pointer -
MyCar mycar = new MyCar;

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

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

when are the time to use the (dot) but not -> (non-pointer) then?

RE: pointer vs non pointer

Hi, I'm not familiar with Java, but I know C++ and there is a big difference in what you wrote.....
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
       CString PalteNumber;

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

To help here is the Stack view Calling ChangePlate


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


Stack view Calling ChangePlate2

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

when ChangePlate2 ends in the stack remains


Did you understand ? Alex

RE: pointer vs non pointer

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.

RE: pointer vs non pointer

The concept of pointers does not exist in Java.  Java only has references.

RE: pointer vs non pointer

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.

RE: pointer vs non pointer

Quote (eric91850):

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):


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.

RE: pointer vs non pointer


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.

Quote (xwb):

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.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close