samedi 28 février 2015

raw, weak_ptr, unique_ptr, shared_ptr etc... how to choose them wisely


There is a lot of pointers in C++ but to be honest in 5 years or so in c++ programmation (specifically with the Qt Framework) I only use the old raw pointer :



SomeKindOfObject *someKindOfObject = new SomeKindOfObject();


I know there is a lot of other "smart" pointers :


shared pointer



shared_ptr<SomeKindofObject> Object;


unique pointer:



unique_ptr<SomeKindofObject> Object;


weak pointer



weak_ptr<SomeKindofObject> Object;


But I don't have the slightest idea of what to do with them and what they can offer me in comparison of raw pointers.


For example I have this class header :



#ifndef LIBRARY
#define LIBRARY

class LIBRARY
{
public:
QList<ItemThatWillBeUsedEveryWhere*> listOfUselessThings; //Permanent list that will be updated from time to time where each items can be modified everywhere in the code
private:
QSettings *_reader //temporary reader that will read something to put in the list and be quickly deleted;
QDialog *_dialog; //dialog that will show something (just for the sake of example)

};

#endif


This is clearly not exhaustive but for each of these 3 pointers is it ok to leave them "raw" or should I use something more appropriate ?


And in the second time, if an employer will read the code, will he be strict on what kind of pointers I use or not ?


Thanks PS: I have no idea if I should've put it in Programmes or StackOverflow





Aucun commentaire:

Enregistrer un commentaire