c++ - Which smart pointer to use in Stack implementation? -
from i've understood correctly:
scoped_ptr: no overhead, cannot copied or moved.
unique_ptr: no overhead, cannot copied, can moved.
shared_ptr: overhead (reference counting), can copied.
having said that, if there need several owners, shared_ptr should used.
now, in program below simple implementation of stack in c++. don't understand type of smart pointer should used.
the reason i'm asking question because both unique_ptr shared_ptr cannot copied , i'm doing in implementation of simple stack. i've commented out //here in program i'm using c++ pointers , if read program you'll see how data getting copied in pretty functions.
gamestatestack.h
#ifndef _h_gamestate_ #define _h_gamestate_ #include <iostream> #include <boost/shared_ptr.hpp> #include <boost/scoped_ptr.hpp> #include <memory> class node { public: std::string gamestate; node * nextgamestate; // here }; class gamestatestack { private: node * _topstate; // here void destory(); public: int gamestatescount; void pushgamestate(std::string element); void popgamestate(); std::string currentgamestate(); gamestatestack(); ~gamestatestack(); }; extern gamestatestack state; #endif
gamestatestack.cpp
#include <iostream> #include <stdlib.h> #include <string> #include <boost/scoped_ptr.hpp> #include <boost/shared_ptr.hpp> #include <memory> #include "gamestatestack.h" #include "template.h" gamestatestack state; gamestatestack::gamestatestack() { _topstate = null; gamestatescount = 0; } gamestatestack::~gamestatestack() { } void gamestatestack::pushgamestate(std::string gamestatename) { node *newtopstate = new node; // here if (_topstate == null) { newtopstate->gamestate = gamestatename; newtopstate->nextgamestate = null; _topstate = newtopstate; gamestatescount++; } else { newtopstate->gamestate = gamestatename; newtopstate->nextgamestate = _topstate; _topstate = newtopstate; gamestatescount++; } } void gamestatestack::popgamestate() { if (_topstate == null) std::cout << "error: no gamestates available pop"; else { node * old = _topstate; // here _topstate = _topstate->nextgamestate; delete(old); gamestatescount--; } } std::string gamestatestack::currentgamestate() { node *temp; // here temp = _topstate; return temp->gamestate; } void gamestatestack::destory() { node *abc; // here delete _topstate; delete abc->nextgamestate; }
here how stack implemented using std::unique_ptr
. note use of std::move()
re-assign std::unique_ptr
leaving original pointing @ nothing.
also, instead of if(topstate == null)
have used more idiomatic if(topstate)
. std::unique_ptr
returns true if points somewhere or false if not.
also standard c++
dictates should not begin variable names leading _
.
#include <string> #include <memory> #include <iostream> struct node { std::string gamestate; std::unique_ptr<node> nextgamestate; ~node() { std::cout << "deleting: " << gamestate << '\n'; } }; class gamestatestack { // should not use _ begin variable names in std c++ std::unique_ptr<node> topstate; int gamestatescount; public: gamestatestack(); void pushgamestate(std::string gamestatename); void popgamestate(); std::string currentgamestate(); void destory(); }; gamestatestack::gamestatestack() : gamestatescount(0) // initialize here { //topstate = null; // no need initialize unique_ptr //gamestatescount = 0; // not here } void gamestatestack::pushgamestate(std::string gamestatename) { std::unique_ptr<node> newtopstate(new node); newtopstate->gamestate = gamestatename; newtopstate->nextgamestate = std::move(topstate); topstate = std::move(newtopstate); gamestatescount++; } void gamestatestack::popgamestate() { if(!topstate) std::cout << "error: no gamestates available pop"; else { topstate = std::move(topstate->nextgamestate); gamestatescount--; } } std::string gamestatestack::currentgamestate() { if(topstate) return topstate->gamestate; return "error: nothing on stack"; // error } void gamestatestack::destory() { // deleting topstate first destroy pointed // node's own unique_ptr<node> nextgamestate // in turn first delete own nextgamestate etc... topstate.reset(); } int main() { gamestatestack stack; std::cout << "\ndestroy test" << '\n'; stack.pushgamestate("a"); stack.pushgamestate("b"); stack.pushgamestate("c"); stack.pushgamestate("d"); stack.pushgamestate("e"); stack.pushgamestate("f"); stack.destory(); std::cout << "\npush-pop test" << '\n'; stack.pushgamestate("a"); stack.pushgamestate("b"); stack.pushgamestate("c"); std::cout << stack.currentgamestate() << '\n'; stack.popgamestate(); stack.pushgamestate("d"); stack.pushgamestate("e"); std::cout << stack.currentgamestate() << '\n'; stack.popgamestate(); std::cout << stack.currentgamestate() << '\n'; stack.popgamestate(); std::cout << stack.currentgamestate() << '\n'; stack.popgamestate(); std::cout << stack.currentgamestate() << '\n'; stack.popgamestate(); std::cout << stack.currentgamestate() << '\n'; stack.popgamestate(); }
Comments
Post a Comment