I am wwondering when is best practice to use pointer.

class Pen{

int main(int argc, char *argv[]){
Pen myPen;

Pen *myPen = new Pen;

These are 2 ways to create myPen object. But what's the main diff between them? When to use it as pointer and when not?



I don't think there is any one right answer. I try not to use pointers unless absolutely necessary because they can cause memory leeks and other problems.

But what's the main diff between them?

The difference is just in the way you are storing them, at the memory level. For example, suppose you had the following memory model:

Address             Contents
               ...                    ...
            |  200   |         |             |
            |  204   |         |    15     |        
            |  208   |       | 'a' 'b' 'c' 'd'|       
                ...                    ...
            | 1000  |         |             |
            | 1004  |         |             |
                ...                    ...

And suppose that memory address 200 is empty, and 1000-1004+ is enough space to store the Pen object that you are declaring.

Now, when you declare Pen myPen1, you are essentially binding the name myPen to a memory location (the location of the pen object), which for the sake of this example, we shall say is memory address 1000 (enough space to store the Pen object). On the other hand, *myPen2 would be bound to memory address 200, where instead of directly pointing to the Pen object, you instead store the location of the actual object, location 1000. So we get something like this:

Address          Contents            Name          Location/Bind
               ...              ...
            |  200   |       |  1000    |         myPen1             1000
            |  204   |       |    15     |         myPen2             200
            |  208   |      | 'a' 'b' 'c' 'd'|
                ...                    ...
            | 1000  |       | (Pen object) |
            | 1004  |       |             |
                ...                    ...

I think that serves the purpose to illustrate the point. However, I think more accurately, myPen would not point directly to the Pen object, but would also actually have a pointer (to a place in memory that the computer can ensure has enough space). And also, they would not point to the same pen object...Regardless, I think the point is clear.

The other thing is, by using "new", you have allocated memory from the heap (as opposed to the stack). But thats a lesson for another time...

When to use it as pointer and when not?

The beauty of this is that you can dereference a pointer, or access the address of a regular variable...So really I don't think there is a hard and fast rule about pointers....Technically you could do everything in C++ without even declaring any pointers, but I guess it just depends on the situation. I would imagine C users would use pointers more often than C++ users...I guess it is whatever you are comfortable with.

If you do use pointers, best be careful that you free up the memory when you are done with them (if you declare them on the heap using the "new" operator).