Created
July 1, 2012 14:07
-
-
Save lynxluna/3028529 to your computer and use it in GitHub Desktop.
QScopedPointer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <QScopedPointer> | |
class PlayerData | |
{ | |
public: | |
PlayerData() : health(100), atk(10), agi(10), name (new char[255]) {} | |
~PlayerData() { delete [] name; } | |
char *name; | |
int health, atk, agi; | |
}; | |
// contoh tanpa smart pointer | |
void doSomePlayerJobDumb() { | |
PlayerData *ptrPlayerData = new PlayerData; | |
-- ptrPlayerData->health; | |
delete ptrPlayerData; | |
} | |
// contoh pointer dengan smart pointer | |
void doSomePlayerJob() { | |
QScopedPointer<PlayerData> ptrPlayerData( new PlayerData ); | |
-- ptrPlayerData->health; | |
// ketika keluar scope, destructor dipanggil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment