In which situations would you need to use a pointer in C++?

Hello, I've been looking back into C++ for about a month now (I've been into C++ for about a year now, Dota 2 stole my life :P), and after all my time with C++, I've never used/meant to use pointers in my code. Could somebody tell me what situations you'd need pointers for?
My thanks.
Besides optional parameters (where the parameter may be nullptr), there are no reasons to use pointers in modern C++. When std::optional finally makes it into the C++ standard library, there will be no remaining reasons to ever use raw pointers in C++. There are, however, plenty of reasons to use the smart pointer wrapper classes like std::unique_ptr. Be aware that std::shared_ptr has a tendency to be overused - generally std::unique_ptr will satisfy all your needs.

If you can't work in a C++11 environment and are forced to stick to C++03, then I suggest replicating the smart pointer wrapper classes or using boost rather than using raw pointers.

If your question was actually about when to use dynamic memory, that's an entirely different discussion.
Last edited on
I would say that the big players are Dynamic Memory and Polymorphism.

It's true, you can use smart pointers to do your memory stuff and so on, however, I think it's a good idea to have a fundamental understanding of how raw pointers play into those things. I don't know, maybe I'm one of these not-so-progressive people.
Imagine that we want to write a program for a library.

The library has books - for example 'Accelerated C++' by Koenig and Moo.
We decide to represent a book as a class.

The library has members; we decide to represent a member as another class.
struct member { /* member stuff */ };

Members may borrow books from the library; after the member returns a book, the same book may be issued to another member. The same member may borrow several books.

For each book, we need to keep track of the identity of the member who currently has the book.

To implement this kind of situation, we need some mechanism for indirection. For instance, if information about the members is stored in a database table with the memberid as the key, we can have:

1
2
3
4
5
6
7
8
9
10
11
struct book
{
    // book stuff title, copy number etc.

    memberid currently_with = invalid_id ; // id of member currently having the book

    // to start with, the book is not with any member: set currently_with = invalid_id ;
    // the book is issued to member with id RBX129k: set currently_with = RBX129k
    // RBX129k returns the book: set currently_with = invalid_id ;
    // the book is later issued to member with id xismn : set currently_with = xismn
};


If member objects are held in memory, we can have:
1
2
3
4
5
6
7
8
9
10
11
struct book
{
    // book stuff title, copy number etc.

    member* currently_with = nullptr ; // address of member currently having the book

    // to start with, the book is not with any member: set currently_with = nullptr ;
    // the book is issued to member RBX129k: set currently_with = address of RBX129k
    // RBX129k returns the book: set currently_with = nullptr ;
    // the book is later issued to member LB : set currently_with = address of LB
};


LB may have borrowed five books; in all of those five books, currently_with would be set to the address of the same LB object.

There is no good way to model this kind of relationship between objects in memory other than by using pointers. Or indirectly using pointers (use something that uses pointers internally - eg. smart pointers or reference wrappers).

Think of pointers as an indirect way of referring to an object.
Typically, we wouldn't use pointers (dumb or smart) unless we couldn't do without them.
* Interfacing with legacy code (primarily C libraries),
* writing usable ABIs,
* implementing data structures,
* implementing allocators,
* doing byte-level manipulations.

Basically any time you want to do things with actual memory, rather than abstract collections of stuff that may or may not be in memory, you'll need pointers.
Dota 2 stole my life :P)


Facebook stole my life
Topic archived. No new replies allowed.