Pointers vs actual object

I've been reading a bunch of tutorials on random stuff, and I constantly see pointers used instead of actual objects. I understand what a pointer is and how it works, but this confuses me.

This is what I mean:
using this:
1
2
MyClass* pMyClass;
MyClass->MyFunction();


instead of this:
1
2
MyClass myClass;
myClass.MyFunction();


What are the benefits (if any) to using the first way instead of the second (or vice versa). Thanks!!
Last edited on
The first way results in undefined behavior. pMyClass doesn't point to a MyClass object.

What are the benefits (if any) to using the first way instead of the second (or vice versa).


There really are no benefits. Use -> if you have a pointer, use . if you don't.
I'm not sure if this will make a difference (please tell me if it will), but what if the first block of code is this:
1
2
MyClass* pMyClass = new MyClass;
MyClass->MyFunction();
Yes, that would take the undefined behavior out of the first code example.
So, are there any new benefits in doing it this way? Or is it just the preference of this programmer?
You have the opposite of benefit: now you must make sure to call delete and worry about exception handling, along with many other downsides. Just use the second method.

If you cannot use the second method, at least use a smart pointer wrapper class like std::unique_ptr or std::shared_ptr.
What about polymorphism?
LB wrote:
If you cannot use the second method, at least use a smart pointer wrapper class like std::unique_ptr or std::shared_ptr.
The general rule is: do not use a pointer unless you know that you can't do without one.
(When writing a program, you would know when you can't do without one.)


A pointer is one way of indirectly referring to an object; and this is quite often useful in practice. For example:
1
2
3
4
5
6
7
8
class some_class { /* ... */ } ;

class my_class { public: some_class* other_object ; } ; 
class your_class { public: some_class* other_object ; } ; 

some_class some_object ;
my_class my_object ; my_object.other_object = &some_object ;
your_class your_object ; your_object.other_object = &some_object ;

Both my_object and your_object can use the same some_object in a shared manner.


A pointer can point to different objects at different points in time. For instance:
1
2
some_class a_different_object ;
my_object.other_object = &a_different_object ;



A pointer can specify that there is no object at all; it may be a null pointer:
my_object.other_object = nullptr ;


As Duoas has already pointed out, pointers are useful when polymorphism is involved - that is the static type of the pointed object is not identical to the dynamic type of the object (this idea may be a little beyond you right now, but you would encounter it soon enough).


In many cases, you may not know if an object is required or how many objects are required at the time you write the code. For instance:
1
2
3
4
Loop:
    Read input from the user.
    If input is 'quit' break out of the loop
    Else Create another object based on the input, and loop again. 

In a situation of this kind, we have to create anonymous (unnamed) objects with a dynamic storage duration (eg. new some_class), which are only reachable via the pointer (that new returns).


One of the most common uses of a pointer (or something that has pointer semantics) is when we want to iterate through a sequence. For example, if we have a list of books (we do not know how many books there are at the time we write the code), and we want to print out the title and author of each of these books, our code would look something like:

1
2
3
4
5
6
initialize a pointer to point to the first book.
repeat:
      print out the title and author of the book pointed to
      make the pointer point to the next book
      if the pointer is not null (there is a next book), go to repeat
      else we are done.    

Have a look at my post in this thread:

http://www.cplusplus.com/forum/general/105593/

which gives some situations where you'd want to use pointers.
Topic archived. No new replies allowed.