Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++: when to use pointers?

After reading some tutorials I came to the conclusion that one should always use pointers for objects. But I have also seen a few exceptions while reading some QT tutorials (http://zetcode.com/gui/qt4/painting/) where QPaint object is created on the stack. So now I am confused. When should I use pointers?

like image 943
Jamol Avatar asked Mar 18 '09 12:03

Jamol


People also ask

What is a pointer and when it should be used?

Pointers are used to store and manage the addresses of dynamically allocated blocks of memory. Such blocks are used to store data objects or arrays of objects. Most structured and object-oriented languages provide an area of memory, called the heap or free store, from which objects are dynamically allocated.

Where are pointers used in C?

Usage of pointer There are many applications of pointers in c language. In c language, we can dynamically allocate memory using malloc() and calloc() functions where the pointer is used. Pointers in c language are widely used in arrays, functions, and structures.

Why pointers are not used in C?

The reason is that pointers are used to bodge into C some vital features which are missing from the original language: arrays, strings, & writeable function parameters.


1 Answers

If you don't know when you should use pointers just don't use them.

It will become apparent when you need to use them, every situation is different. It is not easy to sum up concisely when they should be used. Do not get into the habit of 'always using pointers for objects', that is certainly bad advice.

like image 64
CiscoIPPhone Avatar answered Nov 12 '22 12:11

CiscoIPPhone